Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 1 | /* Copied from gprs_bssgp_pcu.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012 Ivan Klyuchnikov |
Holger Hans Peter Freyther | 8692128 | 2013-08-24 21:26:42 +0200 | [diff] [blame] | 4 | * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu> |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 5 | * Copyright (C) 2013 by Holger Hans Peter Freyther |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (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 General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
Holger Hans Peter Freyther | 67ed34e | 2013-10-17 17:01:54 +0200 | [diff] [blame] | 22 | #include <bts.h> |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 23 | #include <tbf.h> |
Holger Hans Peter Freyther | d11290b | 2013-10-26 17:32:04 +0200 | [diff] [blame] | 24 | #include <rlc.h> |
Holger Hans Peter Freyther | 2db7e7e | 2013-10-26 20:45:35 +0200 | [diff] [blame] | 25 | #include <encoding.h> |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 26 | #include <gprs_rlcmac.h> |
| 27 | #include <gprs_debug.h> |
Holger Hans Peter Freyther | 3dc56a3 | 2013-10-26 21:38:30 +0200 | [diff] [blame] | 28 | #include <gprs_bssgp_pcu.h> |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 29 | |
| 30 | extern "C" { |
| 31 | #include <osmocom/core/msgb.h> |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 32 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | #include <errno.h> |
| 36 | #include <string.h> |
| 37 | |
Holger Hans Peter Freyther | 61a0a04 | 2013-10-26 21:48:38 +0200 | [diff] [blame] | 38 | /* After sending these frames, we poll for ack/nack. */ |
| 39 | #define POLL_ACK_AFTER_FRAMES 20 |
| 40 | |
Holger Hans Peter Freyther | 2db7e7e | 2013-10-26 20:45:35 +0200 | [diff] [blame] | 41 | /* If acknowledgement to downlink assignment should be polled */ |
| 42 | #define POLLING_ASSIGNMENT_DL 1 |
Holger Hans Peter Freyther | ae03f22 | 2013-10-26 21:20:51 +0200 | [diff] [blame] | 43 | #define POLLING_ASSIGNMENT_UL 1 |
Holger Hans Peter Freyther | 2db7e7e | 2013-10-26 20:45:35 +0200 | [diff] [blame] | 44 | |
Holger Hans Peter Freyther | 61a0a04 | 2013-10-26 21:48:38 +0200 | [diff] [blame] | 45 | static const struct gprs_rlcmac_cs gprs_rlcmac_cs[] = { |
| 46 | /* frame length data block max payload */ |
| 47 | { 0, 0, 0 }, |
| 48 | { 23, 23, 20 }, /* CS-1 */ |
| 49 | { 34, 33, 30 }, /* CS-2 */ |
| 50 | { 40, 39, 36 }, /* CS-3 */ |
| 51 | { 54, 53, 50 }, /* CS-4 */ |
| 52 | }; |
| 53 | |
Holger Hans Peter Freyther | 1702f10 | 2013-10-20 08:44:02 +0200 | [diff] [blame] | 54 | extern "C" { |
| 55 | int bssgp_tx_llc_discarded(struct bssgp_bvc_ctx *bctx, uint32_t tlli, |
| 56 | uint8_t num_frames, uint32_t num_octets); |
| 57 | } |
| 58 | |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 59 | extern void *tall_pcu_ctx; |
| 60 | |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 61 | static void tbf_timer_cb(void *_tbf); |
| 62 | |
Holger Hans Peter Freyther | d9262b3 | 2013-10-26 20:12:59 +0200 | [diff] [blame] | 63 | inline gprs_rlcmac_bts *gprs_rlcmac_tbf::bts_data() const |
| 64 | { |
| 65 | return bts->bts_data(); |
| 66 | } |
| 67 | |
Holger Hans Peter Freyther | d1d114f | 2013-08-24 20:46:18 +0200 | [diff] [blame] | 68 | static inline void tbf_update_ms_class(struct gprs_rlcmac_tbf *tbf, |
| 69 | const uint8_t ms_class) |
| 70 | { |
| 71 | if (!tbf->ms_class && ms_class) |
| 72 | tbf->ms_class = ms_class; |
| 73 | } |
| 74 | |
Holger Hans Peter Freyther | d868928 | 2013-08-24 20:51:06 +0200 | [diff] [blame] | 75 | static inline void tbf_assign_imsi(struct gprs_rlcmac_tbf *tbf, |
| 76 | const char *imsi) |
| 77 | { |
| 78 | strncpy(tbf->meas.imsi, imsi, sizeof(tbf->meas.imsi) - 1); |
| 79 | } |
| 80 | |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 81 | static struct gprs_rlcmac_tbf *tbf_lookup_dl(BTS *bts, |
| 82 | const uint32_t tlli, const char *imsi) |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 83 | { |
| 84 | /* TODO: look up by IMSI first, then tlli, then old_tlli */ |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 85 | return bts->tbf_by_tlli(tlli, GPRS_RLCMAC_DL_TBF); |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static int tbf_append_data(struct gprs_rlcmac_tbf *tbf, |
| 89 | struct gprs_rlcmac_bts *bts, |
| 90 | const uint8_t ms_class, |
| 91 | const uint16_t pdu_delay_csec, |
| 92 | const uint8_t *data, const uint16_t len) |
| 93 | { |
| 94 | LOGP(DRLCMAC, LOGL_INFO, "TBF: APPEND TFI: %u TLLI: 0x%08x\n", tbf->tfi, tbf->tlli); |
Holger Hans Peter Freyther | 1c344e2 | 2013-10-16 18:33:18 +0200 | [diff] [blame] | 95 | if (tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE)) { |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 96 | LOGP(DRLCMAC, LOGL_DEBUG, "TBF in WAIT RELEASE state " |
| 97 | "(T3193), so reuse TBF\n"); |
| 98 | memcpy(tbf->llc_frame, data, len); |
| 99 | tbf->llc_length = len; |
| 100 | /* reset rlc states */ |
| 101 | memset(&tbf->dir.dl, 0, sizeof(tbf->dir.dl)); |
| 102 | /* keep to flags */ |
| 103 | tbf->state_flags &= GPRS_RLCMAC_FLAG_TO_MASK; |
| 104 | tbf->state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH); |
Holger Hans Peter Freyther | d1d114f | 2013-08-24 20:46:18 +0200 | [diff] [blame] | 105 | tbf_update_ms_class(tbf, ms_class); |
Holger Hans Peter Freyther | aa9c326 | 2013-10-26 17:49:36 +0200 | [diff] [blame] | 106 | tbf->update(); |
Holger Hans Peter Freyther | 24c1a5b | 2013-10-26 20:27:44 +0200 | [diff] [blame] | 107 | tbf->bts->trigger_dl_ass(tbf, tbf, NULL); |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 108 | } else { |
| 109 | /* the TBF exists, so we must write it in the queue |
| 110 | * we prepend lifetime in front of PDU */ |
| 111 | struct timeval *tv; |
| 112 | struct msgb *llc_msg = msgb_alloc(len + sizeof(*tv), |
| 113 | "llc_pdu_queue"); |
| 114 | if (!llc_msg) |
| 115 | return -ENOMEM; |
| 116 | tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv)); |
| 117 | |
| 118 | uint16_t delay_csec; |
| 119 | if (bts->force_llc_lifetime) |
| 120 | delay_csec = bts->force_llc_lifetime; |
| 121 | else |
| 122 | delay_csec = pdu_delay_csec; |
| 123 | /* keep timestap at 0 for infinite delay */ |
| 124 | if (delay_csec != 0xffff) { |
| 125 | /* calculate timestamp of timeout */ |
| 126 | gettimeofday(tv, NULL); |
| 127 | tv->tv_usec += (delay_csec % 100) * 10000; |
| 128 | tv->tv_sec += delay_csec / 100; |
| 129 | if (tv->tv_usec > 999999) { |
| 130 | tv->tv_usec -= 1000000; |
| 131 | tv->tv_sec++; |
| 132 | } |
| 133 | } |
| 134 | memcpy(msgb_put(llc_msg, len), data, len); |
| 135 | msgb_enqueue(&tbf->llc_queue, llc_msg); |
Holger Hans Peter Freyther | d1d114f | 2013-08-24 20:46:18 +0200 | [diff] [blame] | 136 | tbf_update_ms_class(tbf, ms_class); |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 142 | static int tbf_new_dl_assignment(struct gprs_rlcmac_bts *bts, |
| 143 | const char *imsi, |
| 144 | const uint32_t tlli, const uint8_t ms_class, |
| 145 | const uint8_t *data, const uint16_t len) |
| 146 | { |
| 147 | uint8_t trx, ta, ss; |
| 148 | int8_t use_trx; |
| 149 | struct gprs_rlcmac_tbf *old_tbf, *tbf; |
| 150 | int8_t tfi; /* must be signed */ |
| 151 | int rc; |
| 152 | |
| 153 | /* check for uplink data, so we copy our informations */ |
Holger Hans Peter Freyther | bb20b2c | 2013-08-24 21:22:16 +0200 | [diff] [blame] | 154 | #warning "Do the same look up for IMSI, TLLI and OLD_TLLI" |
| 155 | #warning "Refactor the below lines... into a new method" |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 156 | tbf = bts->bts->tbf_by_tlli(tlli, GPRS_RLCMAC_UL_TBF); |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 157 | if (tbf && tbf->dir.ul.contention_resolution_done |
| 158 | && !tbf->dir.ul.final_ack_sent) { |
Holger Hans Peter Freyther | 96efa70 | 2013-09-29 07:44:39 +0200 | [diff] [blame] | 159 | use_trx = tbf->trx_no; |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 160 | ta = tbf->ta; |
| 161 | ss = 0; |
| 162 | old_tbf = tbf; |
| 163 | } else { |
| 164 | use_trx = -1; |
| 165 | /* we already have an uplink TBF, so we use that TA */ |
| 166 | if (tbf) |
| 167 | ta = tbf->ta; |
| 168 | else { |
| 169 | /* recall TA */ |
Holger Hans Peter Freyther | 111614a | 2013-10-19 20:04:57 +0200 | [diff] [blame] | 170 | rc = bts->bts->timing_advance()->recall(tlli); |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 171 | if (rc < 0) { |
| 172 | LOGP(DRLCMAC, LOGL_NOTICE, "TA unknown" |
| 173 | ", assuming 0\n"); |
| 174 | ta = 0; |
| 175 | } else |
| 176 | ta = rc; |
| 177 | } |
| 178 | ss = 1; /* PCH assignment only allows one timeslot */ |
| 179 | old_tbf = NULL; |
| 180 | } |
| 181 | |
| 182 | // Create new TBF (any TRX) |
Holger Hans Peter Freyther | bcafdf8 | 2013-08-24 21:13:31 +0200 | [diff] [blame] | 183 | #warning "Copy and paste with alloc_ul_tbf" |
Holger Hans Peter Freyther | 70ddde6 | 2013-10-26 19:17:58 +0200 | [diff] [blame] | 184 | tfi = bts->bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx, use_trx); |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 185 | if (tfi < 0) { |
| 186 | LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n"); |
| 187 | /* FIXME: send reject */ |
| 188 | return -EBUSY; |
| 189 | } |
| 190 | /* set number of downlink slots according to multislot class */ |
| 191 | tbf = tbf_alloc(bts, tbf, GPRS_RLCMAC_DL_TBF, tfi, trx, ms_class, ss); |
| 192 | if (!tbf) { |
Holger Hans Peter Freyther | 90b87ea | 2013-10-27 10:38:31 +0100 | [diff] [blame] | 193 | LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n"); |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 194 | /* FIXME: send reject */ |
| 195 | return -EBUSY; |
| 196 | } |
| 197 | tbf->tlli = tlli; |
| 198 | tbf->tlli_valid = 1; |
| 199 | tbf->ta = ta; |
| 200 | |
| 201 | LOGP(DRLCMAC, LOGL_DEBUG, |
| 202 | "TBF: [DOWNLINK] START TFI: %d TLLI: 0x%08x \n", |
| 203 | tbf->tfi, tbf->tlli); |
| 204 | |
| 205 | /* new TBF, so put first frame */ |
| 206 | memcpy(tbf->llc_frame, data, len); |
| 207 | tbf->llc_length = len; |
| 208 | |
| 209 | /* trigger downlink assignment and set state to ASSIGN. |
| 210 | * we don't use old_downlink, so the possible uplink is used |
| 211 | * to trigger downlink assignment. if there is no uplink, |
| 212 | * AGCH is used. */ |
Holger Hans Peter Freyther | 24c1a5b | 2013-10-26 20:27:44 +0200 | [diff] [blame] | 213 | tbf->bts->trigger_dl_ass(tbf, old_tbf, imsi); |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 214 | |
| 215 | /* store IMSI for debugging purpose. TODO: it is more than debugging */ |
| 216 | tbf_assign_imsi(tbf, imsi); |
| 217 | return 0; |
| 218 | } |
| 219 | |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 220 | /** |
| 221 | * TODO: split into unit test-able parts... |
| 222 | */ |
| 223 | int tbf_handle(struct gprs_rlcmac_bts *bts, |
| 224 | const uint32_t tlli, const char *imsi, |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 225 | const uint8_t ms_class, const uint16_t delay_csec, |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 226 | const uint8_t *data, const uint16_t len) |
| 227 | { |
| 228 | struct gprs_rlcmac_tbf *tbf; |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 229 | |
| 230 | /* check for existing TBF */ |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 231 | tbf = tbf_lookup_dl(bts->bts, tlli, imsi); |
Holger Hans Peter Freyther | 31d0df9 | 2013-08-24 20:42:45 +0200 | [diff] [blame] | 232 | if (tbf) { |
| 233 | int rc = tbf_append_data(tbf, bts, ms_class, |
| 234 | delay_csec, data, len); |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 235 | if (rc >= 0) |
| 236 | tbf_assign_imsi(tbf, imsi); |
| 237 | return rc; |
| 238 | } |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 239 | |
Holger Hans Peter Freyther | 443c822 | 2013-08-24 20:59:46 +0200 | [diff] [blame] | 240 | return tbf_new_dl_assignment(bts, imsi, tlli, ms_class, data, len); |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 241 | } |
Holger Hans Peter Freyther | 8692128 | 2013-08-24 21:26:42 +0200 | [diff] [blame] | 242 | |
| 243 | struct gprs_rlcmac_tbf *tbf_alloc_ul(struct gprs_rlcmac_bts *bts, |
| 244 | int8_t use_trx, uint8_t ms_class, |
| 245 | uint32_t tlli, uint8_t ta, struct gprs_rlcmac_tbf *dl_tbf) |
| 246 | { |
| 247 | uint8_t trx; |
| 248 | struct gprs_rlcmac_tbf *tbf; |
| 249 | uint8_t tfi; |
| 250 | |
| 251 | #warning "Copy and paste with tbf_new_dl_assignment" |
| 252 | /* create new TBF, use sme TRX as DL TBF */ |
Holger Hans Peter Freyther | 70ddde6 | 2013-10-26 19:17:58 +0200 | [diff] [blame] | 253 | tfi = bts->bts->tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx, use_trx); |
Holger Hans Peter Freyther | 8692128 | 2013-08-24 21:26:42 +0200 | [diff] [blame] | 254 | if (tfi < 0) { |
Holger Hans Peter Freyther | 90b87ea | 2013-10-27 10:38:31 +0100 | [diff] [blame] | 255 | LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n"); |
Holger Hans Peter Freyther | 8692128 | 2013-08-24 21:26:42 +0200 | [diff] [blame] | 256 | /* FIXME: send reject */ |
| 257 | return NULL; |
| 258 | } |
| 259 | /* use multislot class of downlink TBF */ |
| 260 | tbf = tbf_alloc(bts, dl_tbf, GPRS_RLCMAC_UL_TBF, tfi, trx, ms_class, 0); |
| 261 | if (!tbf) { |
Holger Hans Peter Freyther | 90b87ea | 2013-10-27 10:38:31 +0100 | [diff] [blame] | 262 | LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n"); |
Holger Hans Peter Freyther | 8692128 | 2013-08-24 21:26:42 +0200 | [diff] [blame] | 263 | /* FIXME: send reject */ |
| 264 | return NULL; |
| 265 | } |
| 266 | tbf->tlli = tlli; |
| 267 | tbf->tlli_valid = 1; /* no contention resolution */ |
| 268 | tbf->dir.ul.contention_resolution_done = 1; |
| 269 | tbf->ta = ta; /* use current TA */ |
| 270 | tbf_new_state(tbf, GPRS_RLCMAC_ASSIGN); |
| 271 | tbf->state_flags |= (1 << GPRS_RLCMAC_FLAG_PACCH); |
| 272 | tbf_timer_start(tbf, 3169, bts->t3169, 0); |
| 273 | |
| 274 | return tbf; |
| 275 | } |
Holger Hans Peter Freyther | 964ddb6 | 2013-10-16 17:53:23 +0200 | [diff] [blame] | 276 | |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 277 | static void tbf_unlink_pdch(struct gprs_rlcmac_tbf *tbf) |
| 278 | { |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 279 | struct gprs_rlcmac_pdch *pdch; |
| 280 | int ts; |
| 281 | |
| 282 | if (tbf->direction == GPRS_RLCMAC_UL_TBF) { |
Holger Hans Peter Freyther | 173a240 | 2013-10-16 21:47:45 +0200 | [diff] [blame] | 283 | tbf->trx->ul_tbf[tbf->tfi] = NULL; |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 284 | for (ts = 0; ts < 8; ts++) { |
| 285 | pdch = tbf->pdch[ts]; |
| 286 | if (pdch) |
| 287 | pdch->ul_tbf[tbf->tfi] = NULL; |
| 288 | tbf->pdch[ts] = NULL; |
| 289 | } |
| 290 | } else { |
Holger Hans Peter Freyther | 173a240 | 2013-10-16 21:47:45 +0200 | [diff] [blame] | 291 | tbf->trx->dl_tbf[tbf->tfi] = NULL; |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 292 | for (ts = 0; ts < 8; ts++) { |
| 293 | pdch = tbf->pdch[ts]; |
| 294 | if (pdch) |
| 295 | pdch->dl_tbf[tbf->tfi] = NULL; |
| 296 | tbf->pdch[ts] = NULL; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | void tbf_free(struct gprs_rlcmac_tbf *tbf) |
| 302 | { |
| 303 | struct msgb *msg; |
| 304 | |
| 305 | /* Give final measurement report */ |
| 306 | gprs_rlcmac_rssi_rep(tbf); |
| 307 | gprs_rlcmac_lost_rep(tbf); |
| 308 | |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 309 | debug_diagram(tbf->bts, tbf->diag, "+---------------+"); |
| 310 | debug_diagram(tbf->bts, tbf->diag, "| THE END |"); |
| 311 | debug_diagram(tbf->bts, tbf->diag, "+---------------+"); |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 312 | LOGP(DRLCMAC, LOGL_INFO, "Free %s TBF=%d with TLLI=0x%08x.\n", |
| 313 | (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tbf->tfi, |
| 314 | tbf->tlli); |
| 315 | if (tbf->ul_ass_state != GPRS_RLCMAC_UL_ASS_NONE) |
| 316 | LOGP(DRLCMAC, LOGL_ERROR, "Software error: Pending uplink " |
| 317 | "assignment. This may not happen, because the " |
| 318 | "assignment message never gets transmitted. Please " |
Holger Hans Peter Freyther | 6673005 | 2013-10-26 18:32:13 +0200 | [diff] [blame] | 319 | "be sure not to free in this state. PLEASE FIX!\n"); |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 320 | if (tbf->dl_ass_state != GPRS_RLCMAC_DL_ASS_NONE) |
| 321 | LOGP(DRLCMAC, LOGL_ERROR, "Software error: Pending downlink " |
| 322 | "assignment. This may not happen, because the " |
| 323 | "assignment message never gets transmitted. Please " |
Holger Hans Peter Freyther | 6673005 | 2013-10-26 18:32:13 +0200 | [diff] [blame] | 324 | "be sure not to free in this state. PLEASE FIX!\n"); |
Holger Hans Peter Freyther | 86300bb | 2013-10-26 18:01:35 +0200 | [diff] [blame] | 325 | tbf->stop_timer(); |
Holger Hans Peter Freyther | 782f6dd | 2013-10-26 18:31:51 +0200 | [diff] [blame] | 326 | #warning "TODO: Could/Should generate bssgp_tx_llc_discarded" |
Holger Hans Peter Freyther | 1997787 | 2013-10-27 10:34:31 +0100 | [diff] [blame] | 327 | while ((msg = msgb_dequeue(&tbf->llc_queue))) { |
| 328 | tbf->bts->dropped_frame(); |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 329 | msgb_free(msg); |
Holger Hans Peter Freyther | 1997787 | 2013-10-27 10:34:31 +0100 | [diff] [blame] | 330 | } |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 331 | tbf_unlink_pdch(tbf); |
| 332 | llist_del(&tbf->list); |
Holger Hans Peter Freyther | 1587764 | 2013-10-27 09:50:15 +0100 | [diff] [blame] | 333 | |
| 334 | if (tbf->direction == GPRS_RLCMAC_UL_TBF) |
| 335 | tbf->bts->tbf_ul_freed(); |
| 336 | else |
| 337 | tbf->bts->tbf_dl_freed(); |
| 338 | |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 339 | LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF ends here **********\n"); |
| 340 | talloc_free(tbf); |
| 341 | } |
| 342 | |
Holger Hans Peter Freyther | aa9c326 | 2013-10-26 17:49:36 +0200 | [diff] [blame] | 343 | int gprs_rlcmac_tbf::update() |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 344 | { |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 345 | struct gprs_rlcmac_tbf *ul_tbf = NULL; |
Holger Hans Peter Freyther | aa9c326 | 2013-10-26 17:49:36 +0200 | [diff] [blame] | 346 | struct gprs_rlcmac_bts *bts_data = bts->bts_data(); |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 347 | int rc; |
| 348 | |
| 349 | LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF update **********\n"); |
| 350 | |
Holger Hans Peter Freyther | aa9c326 | 2013-10-26 17:49:36 +0200 | [diff] [blame] | 351 | if (direction != GPRS_RLCMAC_DL_TBF) |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 352 | return -EINVAL; |
| 353 | |
Holger Hans Peter Freyther | aa9c326 | 2013-10-26 17:49:36 +0200 | [diff] [blame] | 354 | if (!ms_class) { |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 355 | LOGP(DRLCMAC, LOGL_DEBUG, "- Cannot update, no class\n"); |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | |
Holger Hans Peter Freyther | aa9c326 | 2013-10-26 17:49:36 +0200 | [diff] [blame] | 359 | ul_tbf = bts->tbf_by_tlli(tlli, GPRS_RLCMAC_UL_TBF); |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 360 | |
Holger Hans Peter Freyther | aa9c326 | 2013-10-26 17:49:36 +0200 | [diff] [blame] | 361 | tbf_unlink_pdch(this); |
| 362 | rc = bts_data->alloc_algorithm(bts_data, ul_tbf, this, bts_data->alloc_algorithm_curst, 0); |
Holger Hans Peter Freyther | 90b87ea | 2013-10-27 10:38:31 +0100 | [diff] [blame] | 363 | /* if no resource */ |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 364 | if (rc < 0) { |
Holger Hans Peter Freyther | 90b87ea | 2013-10-27 10:38:31 +0100 | [diff] [blame] | 365 | LOGP(DRLCMAC, LOGL_ERROR, "No resource after update???\n"); |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 366 | return -rc; |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | int tbf_assign_control_ts(struct gprs_rlcmac_tbf *tbf) |
| 373 | { |
| 374 | if (tbf->control_ts == 0xff) |
| 375 | LOGP(DRLCMAC, LOGL_INFO, "- Setting Control TS %d\n", |
| 376 | tbf->first_common_ts); |
| 377 | else if (tbf->control_ts != tbf->first_common_ts) |
| 378 | LOGP(DRLCMAC, LOGL_INFO, "- Changing Control TS %d\n", |
| 379 | tbf->first_common_ts); |
| 380 | tbf->control_ts = tbf->first_common_ts; |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | static const char *tbf_state_name[] = { |
| 386 | "NULL", |
| 387 | "ASSIGN", |
| 388 | "FLOW", |
| 389 | "FINISHED", |
| 390 | "WAIT RELEASE", |
| 391 | "RELEASING", |
| 392 | }; |
| 393 | |
| 394 | void tbf_new_state(struct gprs_rlcmac_tbf *tbf, |
| 395 | enum gprs_rlcmac_tbf_state state) |
| 396 | { |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 397 | debug_diagram(tbf->bts, tbf->diag, "->%s", tbf_state_name[state]); |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 398 | LOGP(DRLCMAC, LOGL_DEBUG, "%s TBF=%d changes state from %s to %s\n", |
| 399 | (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tbf->tfi, |
| 400 | tbf_state_name[tbf->state], tbf_state_name[state]); |
Holger Hans Peter Freyther | 1c344e2 | 2013-10-16 18:33:18 +0200 | [diff] [blame] | 401 | tbf->set_state(state); |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void tbf_timer_start(struct gprs_rlcmac_tbf *tbf, unsigned int T, |
| 405 | unsigned int seconds, unsigned int microseconds) |
| 406 | { |
| 407 | if (!osmo_timer_pending(&tbf->timer)) |
| 408 | LOGP(DRLCMAC, LOGL_DEBUG, "Starting %s TBF=%d timer %u.\n", |
| 409 | (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", |
| 410 | tbf->tfi, T); |
| 411 | else |
| 412 | LOGP(DRLCMAC, LOGL_DEBUG, "Restarting %s TBF=%d timer %u " |
| 413 | "while old timer %u pending \n", |
| 414 | (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", |
| 415 | tbf->tfi, T, tbf->T); |
| 416 | |
| 417 | tbf->T = T; |
| 418 | tbf->num_T_exp = 0; |
| 419 | |
| 420 | /* Tunning timers can be safely re-scheduled. */ |
| 421 | tbf->timer.data = tbf; |
| 422 | tbf->timer.cb = &tbf_timer_cb; |
| 423 | |
| 424 | osmo_timer_schedule(&tbf->timer, seconds, microseconds); |
| 425 | } |
| 426 | |
Holger Hans Peter Freyther | 86300bb | 2013-10-26 18:01:35 +0200 | [diff] [blame] | 427 | void gprs_rlcmac_tbf::stop_t3191() |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 428 | { |
Holger Hans Peter Freyther | 86300bb | 2013-10-26 18:01:35 +0200 | [diff] [blame] | 429 | return stop_timer(); |
| 430 | } |
| 431 | |
| 432 | void gprs_rlcmac_tbf::stop_timer() |
| 433 | { |
| 434 | if (osmo_timer_pending(&timer)) { |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 435 | LOGP(DRLCMAC, LOGL_DEBUG, "Stopping %s TBF=%d timer %u.\n", |
Holger Hans Peter Freyther | 86300bb | 2013-10-26 18:01:35 +0200 | [diff] [blame] | 436 | (direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi, T); |
| 437 | osmo_timer_del(&timer); |
Holger Hans Peter Freyther | 4556130 | 2013-10-16 17:55:57 +0200 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Holger Hans Peter Freyther | d9262b3 | 2013-10-26 20:12:59 +0200 | [diff] [blame] | 441 | void gprs_rlcmac_tbf::poll_timeout() |
| 442 | { |
| 443 | LOGP(DRLCMAC, LOGL_NOTICE, "Poll timeout for %s TBF=%d\n", |
| 444 | (direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi); |
| 445 | |
| 446 | poll_state = GPRS_RLCMAC_POLL_NONE; |
| 447 | |
| 448 | if (ul_ack_state == GPRS_RLCMAC_UL_ACK_WAIT_ACK) { |
| 449 | if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_UL_ACK))) { |
| 450 | LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling " |
| 451 | "PACKET CONTROL ACK for PACKET UPLINK ACK\n"); |
| 452 | rlcmac_diag(); |
| 453 | state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_UL_ACK); |
| 454 | } |
| 455 | ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE; |
| 456 | debug_diagram(bts, this->diag, "timeout UL-ACK"); |
| 457 | if (state_is(GPRS_RLCMAC_FINISHED)) { |
| 458 | dir.ul.n3103++; |
| 459 | if (dir.ul.n3103 == bts->bts_data()->n3103) { |
| 460 | LOGP(DRLCMAC, LOGL_NOTICE, |
| 461 | "- N3103 exceeded\n"); |
| 462 | debug_diagram(bts, diag, "N3103 exceeded"); |
| 463 | tbf_new_state(this, GPRS_RLCMAC_RELEASING); |
| 464 | tbf_timer_start(this, 3169, bts->bts_data()->t3169, 0); |
| 465 | return; |
| 466 | } |
| 467 | /* reschedule UL ack */ |
| 468 | ul_ack_state = GPRS_RLCMAC_UL_ACK_SEND_ACK; |
| 469 | } |
| 470 | } else if (ul_ass_state == GPRS_RLCMAC_UL_ASS_WAIT_ACK) { |
| 471 | if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_UL_ASS))) { |
| 472 | LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling " |
| 473 | "PACKET CONTROL ACK for PACKET UPLINK " |
| 474 | "ASSIGNMENT.\n"); |
| 475 | rlcmac_diag(); |
| 476 | state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_UL_ASS); |
| 477 | } |
| 478 | ul_ass_state = GPRS_RLCMAC_UL_ASS_NONE; |
| 479 | debug_diagram(bts, diag, "timeout UL-ASS"); |
| 480 | n3105++; |
| 481 | if (n3105 == bts_data()->n3105) { |
| 482 | LOGP(DRLCMAC, LOGL_NOTICE, "- N3105 exceeded\n"); |
| 483 | debug_diagram(bts, diag, "N3105 exceeded"); |
| 484 | tbf_new_state(this, GPRS_RLCMAC_RELEASING); |
| 485 | tbf_timer_start(this, 3195, bts_data()->t3195, 0); |
| 486 | return; |
| 487 | } |
| 488 | /* reschedule UL assignment */ |
| 489 | ul_ass_state = GPRS_RLCMAC_UL_ASS_SEND_ASS; |
| 490 | } else if (dl_ass_state == GPRS_RLCMAC_DL_ASS_WAIT_ACK) { |
| 491 | if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ASS))) { |
| 492 | LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling " |
| 493 | "PACKET CONTROL ACK for PACKET DOWNLINK " |
| 494 | "ASSIGNMENT.\n"); |
| 495 | rlcmac_diag(); |
| 496 | state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_DL_ASS); |
| 497 | } |
| 498 | dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE; |
| 499 | debug_diagram(bts, diag, "timeout DL-ASS"); |
| 500 | n3105++; |
| 501 | if (n3105 == bts->bts_data()->n3105) { |
| 502 | LOGP(DRLCMAC, LOGL_NOTICE, "- N3105 exceeded\n"); |
| 503 | debug_diagram(bts, diag, "N3105 exceeded"); |
| 504 | tbf_new_state(this, GPRS_RLCMAC_RELEASING); |
| 505 | tbf_timer_start(this, 3195, bts_data()->t3195, 0); |
| 506 | return; |
| 507 | } |
| 508 | /* reschedule DL assignment */ |
| 509 | dl_ass_state = GPRS_RLCMAC_DL_ASS_SEND_ASS; |
| 510 | } else if (direction == GPRS_RLCMAC_DL_TBF) { |
| 511 | if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK))) { |
| 512 | LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling " |
| 513 | "PACKET DOWNLINK ACK.\n"); |
| 514 | rlcmac_diag(); |
| 515 | state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK); |
| 516 | } |
| 517 | debug_diagram(bts, diag, "timeout DL-ACK"); |
| 518 | n3105++; |
| 519 | if (n3105 == bts->bts_data()->n3105) { |
| 520 | LOGP(DRLCMAC, LOGL_NOTICE, "- N3105 exceeded\n"); |
| 521 | debug_diagram(bts, diag, "N3105 exceeded"); |
| 522 | tbf_new_state(this, GPRS_RLCMAC_RELEASING); |
| 523 | tbf_timer_start(this, 3195, bts_data()->t3195, 0); |
| 524 | return; |
| 525 | } |
| 526 | /* resend IMM.ASS on CCCH on timeout */ |
| 527 | if ((state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)) |
| 528 | && !(state_flags & (1 << GPRS_RLCMAC_FLAG_DL_ACK))) { |
| 529 | LOGP(DRLCMAC, LOGL_DEBUG, "Re-send dowlink assignment " |
| 530 | "for TBF=%d on PCH (IMSI=%s)\n", tfi, |
| 531 | dir.dl.imsi); |
| 532 | /* send immediate assignment */ |
| 533 | bts->snd_dl_ass(this, 0, dir.dl.imsi); |
| 534 | dir.dl.wait_confirm = 1; |
| 535 | } |
| 536 | } else |
| 537 | LOGP(DRLCMAC, LOGL_ERROR, "- Poll Timeout, but no event!\n"); |
| 538 | } |
| 539 | |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 540 | struct gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts, |
| 541 | struct gprs_rlcmac_tbf *old_tbf, enum gprs_rlcmac_tbf_direction dir, |
| 542 | uint8_t tfi, uint8_t trx, |
| 543 | uint8_t ms_class, uint8_t single_slot) |
| 544 | { |
| 545 | struct gprs_rlcmac_tbf *tbf; |
| 546 | int rc; |
| 547 | |
| 548 | #ifdef DEBUG_DIAGRAM |
| 549 | /* hunt for first free number in diagram */ |
| 550 | int diagram_num; |
| 551 | for (diagram_num = 0; ; diagram_num++) { |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 552 | llist_for_each_entry(tbf, &bts->ul_tbfs, list) { |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 553 | if (tbf->diag == diagram_num) |
| 554 | goto next_diagram; |
| 555 | } |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 556 | llist_for_each_entry(tbf, &bts->dl_tbfs, list) { |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 557 | if (tbf->diag == diagram_num) |
| 558 | goto next_diagram; |
| 559 | } |
| 560 | break; |
| 561 | next_diagram: |
| 562 | continue; |
| 563 | } |
| 564 | #endif |
| 565 | |
| 566 | LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF starts here **********\n"); |
| 567 | LOGP(DRLCMAC, LOGL_INFO, "Allocating %s TBF: TFI=%d TRX=%d " |
| 568 | "MS_CLASS=%d\n", (dir == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", |
| 569 | tfi, trx, ms_class); |
| 570 | |
| 571 | if (trx >= 8 || tfi >= 32) |
| 572 | return NULL; |
| 573 | |
| 574 | tbf = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_tbf); |
| 575 | if (!tbf) |
| 576 | return NULL; |
| 577 | |
Holger Hans Peter Freyther | 9f0c1d2 | 2013-10-19 21:24:34 +0200 | [diff] [blame] | 578 | tbf->bts = bts->bts; |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 579 | #ifdef DEBUG_DIAGRAM |
| 580 | tbf->diag = diagram_num; |
| 581 | #endif |
| 582 | tbf->direction = dir; |
| 583 | tbf->tfi = tfi; |
Holger Hans Peter Freyther | 96efa70 | 2013-09-29 07:44:39 +0200 | [diff] [blame] | 584 | tbf->trx_no = trx; |
Holger Hans Peter Freyther | 743bafa | 2013-09-29 07:50:50 +0200 | [diff] [blame] | 585 | tbf->trx = &bts->trx[trx]; |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 586 | tbf->arfcn = bts->trx[trx].arfcn; |
| 587 | tbf->ms_class = ms_class; |
| 588 | tbf->ws = 64; |
| 589 | tbf->sns = 128; |
| 590 | /* select algorithm */ |
| 591 | rc = bts->alloc_algorithm(bts, old_tbf, tbf, bts->alloc_algorithm_curst, |
| 592 | single_slot); |
Holger Hans Peter Freyther | 90b87ea | 2013-10-27 10:38:31 +0100 | [diff] [blame] | 593 | /* if no resource */ |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 594 | if (rc < 0) { |
| 595 | talloc_free(tbf); |
| 596 | return NULL; |
| 597 | } |
| 598 | /* assign control ts */ |
| 599 | tbf->control_ts = 0xff; |
| 600 | rc = tbf_assign_control_ts(tbf); |
Holger Hans Peter Freyther | 90b87ea | 2013-10-27 10:38:31 +0100 | [diff] [blame] | 601 | /* if no resource */ |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 602 | if (rc < 0) { |
| 603 | talloc_free(tbf); |
| 604 | return NULL; |
| 605 | } |
| 606 | |
| 607 | /* set timestamp */ |
| 608 | gettimeofday(&tbf->meas.dl_bw_tv, NULL); |
| 609 | gettimeofday(&tbf->meas.rssi_tv, NULL); |
| 610 | gettimeofday(&tbf->meas.dl_loss_tv, NULL); |
| 611 | |
| 612 | INIT_LLIST_HEAD(&tbf->llc_queue); |
Holger Hans Peter Freyther | 1587764 | 2013-10-27 09:50:15 +0100 | [diff] [blame] | 613 | if (dir == GPRS_RLCMAC_UL_TBF) { |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 614 | llist_add(&tbf->list, &bts->ul_tbfs); |
Holger Hans Peter Freyther | 1587764 | 2013-10-27 09:50:15 +0100 | [diff] [blame] | 615 | tbf->bts->tbf_ul_created(); |
| 616 | } else { |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 617 | llist_add(&tbf->list, &bts->dl_tbfs); |
Holger Hans Peter Freyther | 1587764 | 2013-10-27 09:50:15 +0100 | [diff] [blame] | 618 | tbf->bts->tbf_dl_created(); |
| 619 | } |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 620 | |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 621 | debug_diagram(bts->bts, tbf->diag, "+-----------------+"); |
| 622 | debug_diagram(bts->bts, tbf->diag, "|NEW %s TBF TFI=%2d|", |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 623 | (dir == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi); |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 624 | debug_diagram(bts->bts, tbf->diag, "+-----------------+"); |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 625 | |
| 626 | return tbf; |
| 627 | } |
| 628 | |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 629 | static void tbf_timer_cb(void *_tbf) |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 630 | { |
| 631 | struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)_tbf; |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 632 | tbf->handle_timeout(); |
| 633 | } |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 634 | |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 635 | void gprs_rlcmac_tbf::handle_timeout() |
| 636 | { |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 637 | LOGP(DRLCMAC, LOGL_DEBUG, "%s TBF=%d timer %u expired.\n", |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 638 | (direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi, T); |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 639 | |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 640 | num_T_exp++; |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 641 | |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 642 | switch (T) { |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 643 | case 0: /* assignment */ |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 644 | if ((state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH))) { |
| 645 | if (state_is(GPRS_RLCMAC_ASSIGN)) { |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 646 | LOGP(DRLCMAC, LOGL_NOTICE, "Releasing due to " |
| 647 | "PACCH assignment timeout.\n"); |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 648 | tbf_free(this); |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 649 | } else |
| 650 | LOGP(DRLCMAC, LOGL_ERROR, "Error: TBF is not " |
| 651 | "in assign state\n"); |
| 652 | } |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 653 | if ((state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH))) { |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 654 | /* change state to FLOW, so scheduler will start transmission */ |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 655 | dir.dl.wait_confirm = 0; |
| 656 | if (state_is(GPRS_RLCMAC_ASSIGN)) { |
| 657 | tbf_new_state(this, GPRS_RLCMAC_FLOW); |
| 658 | tbf_assign_control_ts(this); |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 659 | } else |
| 660 | LOGP(DRLCMAC, LOGL_NOTICE, "Continue flow after " |
| 661 | "IMM.ASS confirm\n"); |
| 662 | } |
| 663 | break; |
| 664 | case 3169: |
| 665 | case 3191: |
| 666 | case 3195: |
| 667 | LOGP(DRLCMAC, LOGL_NOTICE, "TBF T%d timeout during " |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 668 | "transsmission\n", T); |
| 669 | rlcmac_diag(); |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 670 | /* fall through */ |
| 671 | case 3193: |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 672 | if (T == 3193) |
| 673 | debug_diagram(bts, diag, "T3193 timeout"); |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 674 | LOGP(DRLCMAC, LOGL_DEBUG, "TBF will be freed due to timeout\n"); |
| 675 | /* free TBF */ |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 676 | tbf_free(this); |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 677 | break; |
| 678 | default: |
Holger Hans Peter Freyther | af8094d | 2013-10-26 17:56:15 +0200 | [diff] [blame] | 679 | LOGP(DRLCMAC, LOGL_ERROR, "Timer expired in unknown mode: %u\n", T); |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
| 683 | int gprs_rlcmac_tbf::rlcmac_diag() |
| 684 | { |
| 685 | if ((state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH))) |
| 686 | LOGP(DRLCMAC, LOGL_NOTICE, "- Assignment was on CCCH\n"); |
| 687 | if ((state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH))) |
| 688 | LOGP(DRLCMAC, LOGL_NOTICE, "- Assignment was on PACCH\n"); |
| 689 | if ((state_flags & (1 << GPRS_RLCMAC_FLAG_UL_DATA))) |
| 690 | LOGP(DRLCMAC, LOGL_NOTICE, "- Uplink data was received\n"); |
| 691 | else if (direction == GPRS_RLCMAC_UL_TBF) |
| 692 | LOGP(DRLCMAC, LOGL_NOTICE, "- No uplink data received yet\n"); |
| 693 | if ((state_flags & (1 << GPRS_RLCMAC_FLAG_DL_ACK))) |
| 694 | LOGP(DRLCMAC, LOGL_NOTICE, "- Downlink ACK was received\n"); |
| 695 | else if (direction == GPRS_RLCMAC_DL_TBF) |
| 696 | LOGP(DRLCMAC, LOGL_NOTICE, "- No downlink ACK received yet\n"); |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
Holger Hans Peter Freyther | 1702f10 | 2013-10-20 08:44:02 +0200 | [diff] [blame] | 701 | struct msgb *gprs_rlcmac_tbf::llc_dequeue(bssgp_bvc_ctx *bctx) |
| 702 | { |
| 703 | struct msgb *msg; |
| 704 | struct timeval *tv, tv_now; |
| 705 | uint32_t octets = 0, frames = 0; |
| 706 | |
| 707 | gettimeofday(&tv_now, NULL); |
| 708 | |
| 709 | while ((msg = msgb_dequeue(&llc_queue))) { |
| 710 | tv = (struct timeval *)msg->data; |
| 711 | msgb_pull(msg, sizeof(*tv)); |
| 712 | if (tv->tv_sec /* not infinite */ |
| 713 | && (tv_now.tv_sec > tv->tv_sec /* and secs expired */ |
| 714 | || (tv_now.tv_sec == tv->tv_sec /* .. or if secs equal .. */ |
| 715 | && tv_now.tv_usec > tv->tv_usec))) { /* .. usecs expired */ |
| 716 | LOGP(DRLCMACDL, LOGL_NOTICE, "Discarding LLC PDU of " |
| 717 | "DL TBF=%d, because lifetime limit reached\n", |
| 718 | tfi); |
Holger Hans Peter Freyther | 1997787 | 2013-10-27 10:34:31 +0100 | [diff] [blame] | 719 | bts->timedout_frame(); |
Holger Hans Peter Freyther | 1702f10 | 2013-10-20 08:44:02 +0200 | [diff] [blame] | 720 | frames++; |
| 721 | octets += msg->len; |
| 722 | msgb_free(msg); |
| 723 | continue; |
| 724 | } |
| 725 | break; |
| 726 | } |
| 727 | |
| 728 | if (frames) { |
| 729 | if (frames > 0xff) |
| 730 | frames = 0xff; |
| 731 | if (octets > 0xffffff) |
| 732 | octets = 0xffffff; |
| 733 | bssgp_tx_llc_discarded(bctx, tlli, frames, octets); |
| 734 | } |
| 735 | |
| 736 | return msg; |
| 737 | } |
| 738 | |
| 739 | void gprs_rlcmac_tbf::update_llc_frame(struct msgb *msg) |
| 740 | { |
| 741 | /* TODO: bounds check */ |
| 742 | memcpy(llc_frame, msg->data, msg->len); |
| 743 | llc_length = msg->len; |
| 744 | } |
| 745 | |
Holger Hans Peter Freyther | d11290b | 2013-10-26 17:32:04 +0200 | [diff] [blame] | 746 | /* |
| 747 | * Store received block data in LLC message(s) and forward to SGSN |
| 748 | * if complete. |
| 749 | */ |
| 750 | int gprs_rlcmac_tbf::assemble_forward_llc(uint8_t *data, uint8_t len) |
| 751 | { |
| 752 | struct rlc_ul_header *rh = (struct rlc_ul_header *)data; |
| 753 | uint8_t e, m; |
| 754 | struct rlc_li_field *li; |
| 755 | uint8_t frame_offset[16], offset = 0, chunk; |
| 756 | int i, frames = 0; |
| 757 | |
| 758 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Assembling frames: (len=%d)\n", len); |
| 759 | |
| 760 | data += 3; |
| 761 | len -= 3; |
| 762 | e = rh->e; /* if extended */ |
| 763 | m = 1; /* more frames, that means: the first frame */ |
| 764 | |
| 765 | /* Parse frame offsets from length indicator(s), if any. */ |
| 766 | while (1) { |
| 767 | if (frames == (int)sizeof(frame_offset)) { |
| 768 | LOGP(DRLCMACUL, LOGL_ERROR, "Too many frames in " |
| 769 | "block\n"); |
| 770 | return -EINVAL; |
| 771 | } |
| 772 | frame_offset[frames++] = offset; |
| 773 | LOGP(DRLCMACUL, LOGL_DEBUG, "-- Frame %d starts at offset " |
| 774 | "%d\n", frames, offset); |
| 775 | if (!len) |
| 776 | break; |
| 777 | /* M == 0 and E == 0 is not allowed in this version. */ |
| 778 | if (!m && !e) { |
| 779 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TBF=%d " |
| 780 | "ignored, because M='0' and E='0'.\n", |
| 781 | this->tfi); |
| 782 | return 0; |
| 783 | } |
| 784 | /* no more frames in this segment */ |
| 785 | if (e) { |
| 786 | break; |
| 787 | } |
| 788 | /* There is a new frame and an LI that delimits it. */ |
| 789 | if (m) { |
| 790 | li = (struct rlc_li_field *)data; |
| 791 | LOGP(DRLCMACUL, LOGL_DEBUG, "-- Delimiter len=%d\n", |
| 792 | li->li); |
| 793 | /* Special case: LI == 0 |
| 794 | * If the last segment would fit precisely into the |
| 795 | * rest of the RLC MAC block, there would be no way |
| 796 | * to delimit that this segment ends and is not |
| 797 | * continued in the next block. |
| 798 | * The special LI (0) is used to force the segment to |
| 799 | * extend into the next block, so it is delimited there. |
| 800 | * This LI must be skipped. Also it is the last LI. |
| 801 | */ |
| 802 | if (li->li == 0) { |
| 803 | data++; |
| 804 | len--; |
| 805 | m = 1; /* M is ignored, we know there is more */ |
| 806 | break; /* handle E as '1', so we break! */ |
| 807 | } |
| 808 | e = li->e; |
| 809 | m = li->m; |
| 810 | offset += li->li; |
| 811 | data++; |
| 812 | len--; |
| 813 | continue; |
| 814 | } |
| 815 | } |
| 816 | if (!m) { |
| 817 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Last frame carries spare " |
| 818 | "data\n"); |
| 819 | } |
| 820 | |
| 821 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Data length after length fields: %d\n", |
| 822 | len); |
| 823 | /* TLLI */ |
| 824 | if (rh->ti) { |
| 825 | if (len < 4) { |
| 826 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TLLI out of " |
| 827 | "frame border\n"); |
| 828 | return -EINVAL; |
| 829 | } |
| 830 | data += 4; |
| 831 | len -= 4; |
| 832 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Length after skipping TLLI: " |
| 833 | "%d\n", len); |
| 834 | } |
| 835 | |
| 836 | /* PFI */ |
| 837 | if (rh->pi) { |
| 838 | LOGP(DRLCMACUL, LOGL_ERROR, "ERROR: PFI not supported, " |
| 839 | "please disable in SYSTEM INFORMATION\n"); |
| 840 | if (len < 1) { |
| 841 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA PFI out of " |
| 842 | "frame border\n"); |
| 843 | return -EINVAL; |
| 844 | } |
| 845 | data++; |
| 846 | len--; |
| 847 | LOGP(DRLCMACUL, LOGL_DEBUG, "- Length after skipping PFI: " |
| 848 | "%d\n", len); |
| 849 | } |
| 850 | |
| 851 | /* Now we have: |
| 852 | * - a list of frames offsets: frame_offset[] |
| 853 | * - number of frames: i |
| 854 | * - m == 0: Last frame carries spare data (end of TBF). |
| 855 | */ |
| 856 | |
| 857 | /* Check if last offset would exceed frame. */ |
| 858 | if (offset > len) { |
| 859 | LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TBF=%d ignored, " |
| 860 | "because LI delimits data that exceeds block size.\n", |
| 861 | this->tfi); |
| 862 | return -EINVAL; |
| 863 | } |
| 864 | |
| 865 | /* create LLC frames */ |
| 866 | for (i = 0; i < frames; i++) { |
| 867 | /* last frame ? */ |
| 868 | if (i == frames - 1) { |
| 869 | /* no more data in last frame */ |
| 870 | if (!m) |
| 871 | break; |
| 872 | /* data until end of frame */ |
| 873 | chunk = len - frame_offset[i]; |
| 874 | } else { |
| 875 | /* data until next frame */ |
| 876 | chunk = frame_offset[i + 1] - frame_offset[i]; |
| 877 | } |
| 878 | LOGP(DRLCMACUL, LOGL_DEBUG, "-- Appending chunk (len=%d) to " |
| 879 | "frame at %d.\n", chunk, this->llc_index); |
| 880 | if (this->llc_index + chunk > LLC_MAX_LEN) { |
| 881 | LOGP(DRLCMACUL, LOGL_NOTICE, "LLC frame exceeds " |
| 882 | "maximum size.\n"); |
| 883 | chunk = LLC_MAX_LEN - this->llc_index; |
| 884 | } |
| 885 | memcpy(this->llc_frame + this->llc_index, data + frame_offset[i], |
| 886 | chunk); |
| 887 | this->llc_index += chunk; |
| 888 | /* not last frame. */ |
| 889 | if (i != frames - 1) { |
| 890 | /* send frame to SGSN */ |
| 891 | LOGP(DRLCMACUL, LOGL_INFO, "Complete UL frame for " |
| 892 | "TBF=%d: len=%d\n", this->tfi, this->llc_index); |
| 893 | gprs_rlcmac_tx_ul_ud(this); |
| 894 | this->llc_index = 0; /* reset frame space */ |
| 895 | /* also check if CV==0, because the frame may fill up the |
| 896 | * block precisely, then it is also complete. normally the |
| 897 | * frame would be extended into the next block with a 0-length |
| 898 | * delimiter added to this block. */ |
| 899 | } else if (rh->cv == 0) { |
| 900 | /* send frame to SGSN */ |
| 901 | LOGP(DRLCMACUL, LOGL_INFO, "Complete UL frame for " |
| 902 | "TBF=%d that fits precisely in last block: " |
| 903 | "len=%d\n", this->tfi, this->llc_index); |
| 904 | gprs_rlcmac_tx_ul_ud(this); |
| 905 | this->llc_index = 0; /* reset frame space */ |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
Holger Hans Peter Freyther | 61a0a04 | 2013-10-26 21:48:38 +0200 | [diff] [blame] | 912 | /* |
| 913 | * Create DL data block |
| 914 | * The messages are fragmented and forwarded as data blocks. |
| 915 | */ |
| 916 | struct msgb *gprs_rlcmac_tbf::create_dl_acked_block(uint32_t fn, uint8_t ts) |
| 917 | { |
| 918 | struct rlc_dl_header *rh; |
| 919 | struct rlc_li_field *li; |
| 920 | uint8_t block_length; /* total length of block, including spare bits */ |
| 921 | uint8_t block_data; /* usable data of block, w/o spare bits, inc. MAC */ |
| 922 | struct msgb *msg, *dl_msg; |
| 923 | uint8_t bsn; |
| 924 | uint16_t mod_sns = sns - 1; |
| 925 | uint16_t mod_sns_half = (sns >> 1) - 1; |
| 926 | uint16_t index; |
| 927 | uint8_t *delimiter, *data, *e_pointer; |
| 928 | uint8_t len; |
| 929 | uint16_t space, chunk; |
| 930 | int first_fin_ack = 0; |
| 931 | |
| 932 | LOGP(DRLCMACDL, LOGL_DEBUG, "DL DATA TBF=%d downlink (V(A)==%d .. " |
| 933 | "V(S)==%d)\n", tfi, dir.dl.v_a, dir.dl.v_s); |
| 934 | |
| 935 | do_resend: |
| 936 | /* check if there is a block with negative acknowledgement */ |
| 937 | for (bsn = dir.dl.v_a; bsn != dir.dl.v_s; |
| 938 | bsn = (bsn + 1) & mod_sns) { |
| 939 | index = (bsn & mod_sns_half); |
| 940 | if (dir.dl.v_b[index] == 'N' |
| 941 | || dir.dl.v_b[index] == 'X') { |
| 942 | LOGP(DRLCMACDL, LOGL_DEBUG, "- Resending BSN %d\n", |
| 943 | bsn); |
| 944 | /* re-send block with negative aknowlegement */ |
| 945 | dir.dl.v_b[index] = 'U'; /* unacked */ |
| 946 | goto tx_block; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /* if the window has stalled, or transfer is complete, |
| 951 | * send an unacknowledged block */ |
| 952 | if (state_is(GPRS_RLCMAC_FINISHED) |
| 953 | || ((dir.dl.v_s - dir.dl.v_a) & mod_sns) == ws) { |
| 954 | int resend = 0; |
| 955 | |
| 956 | if (state_is(GPRS_RLCMAC_FINISHED)) |
| 957 | LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, " |
| 958 | "because all blocks have been transmitted.\n", |
| 959 | dir.dl.v_a); |
| 960 | else |
| 961 | LOGP(DRLCMACDL, LOGL_NOTICE, "- Restarting at BSN %d, " |
| 962 | "because all window is stalled.\n", |
| 963 | dir.dl.v_a); |
| 964 | /* If V(S) == V(A) and finished state, we would have received |
| 965 | * acknowledgement of all transmitted block. In this case we |
| 966 | * would have transmitted the final block, and received ack |
| 967 | * from MS. But in this case we did not receive the final ack |
| 968 | * indication from MS. This should never happen if MS works |
| 969 | * correctly. */ |
| 970 | if (dir.dl.v_s == dir.dl.v_a) { |
| 971 | LOGP(DRLCMACDL, LOGL_DEBUG, "- MS acked all blocks, " |
| 972 | "so we re-transmit final block!\n"); |
| 973 | /* we just send final block again */ |
| 974 | index = ((dir.dl.v_s - 1) & mod_sns_half); |
| 975 | goto tx_block; |
| 976 | } |
| 977 | |
| 978 | /* cycle through all unacked blocks */ |
| 979 | for (bsn = dir.dl.v_a; bsn != dir.dl.v_s; |
| 980 | bsn = (bsn + 1) & mod_sns) { |
| 981 | index = (bsn & mod_sns_half); |
| 982 | if (dir.dl.v_b[index] == 'U') { |
| 983 | /* mark to be re-send */ |
| 984 | dir.dl.v_b[index] = 'X'; |
| 985 | resend++; |
| 986 | } |
| 987 | } |
| 988 | /* At this point there should be at leasst one unacked block |
| 989 | * to be resent. If not, this is an software error. */ |
| 990 | if (resend == 0) { |
| 991 | LOGP(DRLCMACDL, LOGL_ERROR, "Software error: " |
| 992 | "There are no unacknowledged blocks, but V(A) " |
| 993 | " != V(S). PLEASE FIX!\n"); |
| 994 | /* we just send final block again */ |
| 995 | index = ((dir.dl.v_s - 1) & mod_sns_half); |
| 996 | goto tx_block; |
| 997 | } |
| 998 | goto do_resend; |
| 999 | } |
| 1000 | |
| 1001 | LOGP(DRLCMACDL, LOGL_DEBUG, "- Sending new block at BSN %d\n", |
| 1002 | dir.dl.v_s); |
| 1003 | |
| 1004 | /* now we still have untransmitted LLC data, so we fill mac block */ |
| 1005 | index = dir.dl.v_s & mod_sns_half; |
| 1006 | data = rlc_block[index]; |
| 1007 | #warning "Selection of the CS doesn't belong here" |
| 1008 | if (cs == 0) { |
| 1009 | cs = bts_data()->initial_cs_dl; |
| 1010 | if (cs < 1 || cs > 4) |
| 1011 | cs = 1; |
| 1012 | } |
| 1013 | block_length = gprs_rlcmac_cs[cs].block_length; |
| 1014 | block_data = gprs_rlcmac_cs[cs].block_data; |
| 1015 | memset(data, 0x2b, block_data); /* spare bits will be left 0 */ |
| 1016 | rh = (struct rlc_dl_header *)data; |
| 1017 | rh->pt = 0; /* Data Block */ |
| 1018 | rh->rrbp = rh->s_p = 0; /* Polling, set later, if required */ |
| 1019 | rh->usf = 7; /* will be set at scheduler */ |
| 1020 | rh->pr = 0; /* FIXME: power reduction */ |
| 1021 | rh->tfi = tfi; /* TFI */ |
| 1022 | rh->fbi = 0; /* Final Block Indicator, set late, if true */ |
| 1023 | rh->bsn = dir.dl.v_s; /* Block Sequence Number */ |
| 1024 | rh->e = 0; /* Extension bit, maybe set later */ |
| 1025 | e_pointer = data + 2; /* points to E of current chunk */ |
| 1026 | data += 3; |
| 1027 | delimiter = data; /* where next length header would be stored */ |
| 1028 | space = block_data - 3; |
| 1029 | while (1) { |
| 1030 | chunk = llc_length - llc_index; |
| 1031 | /* if chunk will exceed block limit */ |
| 1032 | if (chunk > space) { |
| 1033 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d " |
| 1034 | "larger than space (%d) left in block: copy " |
| 1035 | "only remaining space, and we are done\n", |
| 1036 | chunk, space); |
| 1037 | /* block is filled, so there is no extension */ |
| 1038 | *e_pointer |= 0x01; |
| 1039 | /* fill only space */ |
| 1040 | memcpy(data, llc_frame + llc_index, space); |
| 1041 | /* incement index */ |
| 1042 | llc_index += space; |
| 1043 | /* return data block as message */ |
| 1044 | break; |
| 1045 | } |
| 1046 | /* if FINAL chunk would fit precisely in space left */ |
| 1047 | if (chunk == space && llist_empty(&llc_queue)) { |
| 1048 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d " |
| 1049 | "would exactly fit into space (%d): because " |
| 1050 | "this is a final block, we don't add length " |
| 1051 | "header, and we are done\n", chunk, space); |
| 1052 | LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for " |
| 1053 | "TBF=%d that fits precisely in last block: " |
| 1054 | "len=%d\n", tfi, llc_length); |
| 1055 | gprs_rlcmac_dl_bw(this, llc_length); |
| 1056 | /* block is filled, so there is no extension */ |
| 1057 | *e_pointer |= 0x01; |
| 1058 | /* fill space */ |
| 1059 | memcpy(data, llc_frame + llc_index, space); |
| 1060 | /* reset LLC frame */ |
| 1061 | llc_index = llc_length = 0; |
| 1062 | /* final block */ |
| 1063 | rh->fbi = 1; /* we indicate final block */ |
| 1064 | tbf_new_state(this, GPRS_RLCMAC_FINISHED); |
| 1065 | /* return data block as message */ |
| 1066 | break; |
| 1067 | } |
| 1068 | /* if chunk would fit exactly in space left */ |
| 1069 | if (chunk == space) { |
| 1070 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d " |
| 1071 | "would exactly fit into space (%d): add length " |
| 1072 | "header with LI=0, to make frame extend to " |
| 1073 | "next block, and we are done\n", chunk, space); |
| 1074 | /* make space for delimiter */ |
| 1075 | if (delimiter != data) |
| 1076 | memcpy(delimiter + 1, delimiter, |
| 1077 | data - delimiter); |
| 1078 | data++; |
| 1079 | space--; |
| 1080 | /* add LI with 0 length */ |
| 1081 | li = (struct rlc_li_field *)delimiter; |
| 1082 | li->e = 1; /* not more extension */ |
| 1083 | li->m = 0; /* shall be set to 0, in case of li = 0 */ |
| 1084 | li->li = 0; /* chunk fills the complete space */ |
| 1085 | // no need to set e_pointer nor increase delimiter |
| 1086 | /* fill only space, which is 1 octet less than chunk */ |
| 1087 | memcpy(data, llc_frame + llc_index, space); |
| 1088 | /* incement index */ |
| 1089 | llc_index += space; |
| 1090 | /* return data block as message */ |
| 1091 | break; |
| 1092 | } |
| 1093 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less " |
| 1094 | "than remaining space (%d): add length header to " |
| 1095 | "to delimit LLC frame\n", chunk, space); |
| 1096 | /* the LLC frame chunk ends in this block */ |
| 1097 | /* make space for delimiter */ |
| 1098 | if (delimiter != data) |
| 1099 | memcpy(delimiter + 1, delimiter, data - delimiter); |
| 1100 | data++; |
| 1101 | space--; |
| 1102 | /* add LI to delimit frame */ |
| 1103 | li = (struct rlc_li_field *)delimiter; |
| 1104 | li->e = 0; /* Extension bit, maybe set later */ |
| 1105 | li->m = 0; /* will be set later, if there is more LLC data */ |
| 1106 | li->li = chunk; /* length of chunk */ |
| 1107 | e_pointer = delimiter; /* points to E of current delimiter */ |
| 1108 | delimiter++; |
| 1109 | /* copy (rest of) LLC frame to space */ |
| 1110 | memcpy(data, llc_frame + llc_index, chunk); |
| 1111 | data += chunk; |
| 1112 | space -= chunk; |
| 1113 | LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for TBF=%d: " |
| 1114 | "len=%d\n", tfi, llc_length); |
| 1115 | gprs_rlcmac_dl_bw(this, llc_length); |
| 1116 | /* reset LLC frame */ |
| 1117 | llc_index = llc_length = 0; |
| 1118 | /* dequeue next LLC frame, if any */ |
| 1119 | msg = llc_dequeue(gprs_bssgp_pcu_current_bctx()); |
| 1120 | if (msg) { |
| 1121 | LOGP(DRLCMACDL, LOGL_INFO, "- Dequeue next LLC for " |
| 1122 | "TBF=%d (len=%d)\n", tfi, msg->len); |
| 1123 | update_llc_frame(msg); |
| 1124 | msgb_free(msg); |
| 1125 | } |
| 1126 | /* if we have more data and we have space left */ |
| 1127 | if (space > 0 && llc_length) { |
| 1128 | li->m = 1; /* we indicate more frames to follow */ |
| 1129 | continue; |
| 1130 | } |
| 1131 | /* if we don't have more LLC frames */ |
| 1132 | if (!llc_length) { |
| 1133 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we " |
| 1134 | "done.\n"); |
| 1135 | li->e = 1; /* we cannot extend */ |
| 1136 | rh->fbi = 1; /* we indicate final block */ |
| 1137 | first_fin_ack = 1; |
| 1138 | /* + 1 indicates: first final ack */ |
| 1139 | tbf_new_state(this, GPRS_RLCMAC_FINISHED); |
| 1140 | break; |
| 1141 | } |
| 1142 | /* we have no space left */ |
| 1143 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are " |
| 1144 | "done.\n"); |
| 1145 | li->e = 1; /* we cannot extend */ |
| 1146 | break; |
| 1147 | } |
| 1148 | LOGP(DRLCMACDL, LOGL_DEBUG, "data block: %s\n", |
| 1149 | osmo_hexdump(rlc_block[index], block_length)); |
| 1150 | rlc_block_len[index] = block_length; |
| 1151 | /* raise send state and set ack state array */ |
| 1152 | dir.dl.v_b[index] = 'U'; /* unacked */ |
| 1153 | dir.dl.v_s = (dir.dl.v_s + 1) & mod_sns; /* inc send state */ |
| 1154 | |
| 1155 | tx_block: |
| 1156 | /* from this point on, new block is sent or old block is resent */ |
| 1157 | |
| 1158 | /* get data and header from current block */ |
| 1159 | data = rlc_block[index]; |
| 1160 | len = rlc_block_len[index]; |
| 1161 | rh = (struct rlc_dl_header *)data; |
| 1162 | |
| 1163 | /* Clear Polling, if still set in history buffer */ |
| 1164 | rh->s_p = 0; |
| 1165 | |
| 1166 | /* poll after POLL_ACK_AFTER_FRAMES frames, or when final block is tx. |
| 1167 | */ |
| 1168 | if (dir.dl.tx_counter >= POLL_ACK_AFTER_FRAMES || first_fin_ack) { |
| 1169 | if (first_fin_ack) { |
| 1170 | LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack " |
| 1171 | "polling, because first final block sent.\n"); |
| 1172 | } else { |
| 1173 | LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack " |
| 1174 | "polling, because %d blocks sent.\n", |
| 1175 | POLL_ACK_AFTER_FRAMES); |
| 1176 | } |
| 1177 | /* scheduling not possible, because: */ |
| 1178 | if (poll_state != GPRS_RLCMAC_POLL_NONE) |
| 1179 | LOGP(DRLCMAC, LOGL_DEBUG, "Polling is already " |
| 1180 | "sheduled for TBF=%d, so we must wait for " |
| 1181 | "requesting downlink ack\n", tfi); |
| 1182 | else if (control_ts != ts) |
| 1183 | LOGP(DRLCMAC, LOGL_DEBUG, "Polling cannot be " |
| 1184 | "sheduled in this TS %d, waiting for " |
| 1185 | "TS %d\n", ts, control_ts); |
| 1186 | else if (bts->sba()->find(trx_no, ts, (fn + 13) % 2715648)) |
| 1187 | LOGP(DRLCMAC, LOGL_DEBUG, "Polling cannot be " |
| 1188 | "sheduled, because single block alllocation " |
| 1189 | "already exists\n"); |
| 1190 | else { |
| 1191 | LOGP(DRLCMAC, LOGL_DEBUG, "Polling sheduled in this " |
| 1192 | "TS %d\n", ts); |
| 1193 | dir.dl.tx_counter = 0; |
| 1194 | /* start timer whenever we send the final block */ |
| 1195 | if (rh->fbi == 1) |
| 1196 | tbf_timer_start(this, 3191, bts_data()->t3191, 0); |
| 1197 | |
| 1198 | /* schedule polling */ |
| 1199 | poll_state = GPRS_RLCMAC_POLL_SCHED; |
| 1200 | poll_fn = (fn + 13) % 2715648; |
| 1201 | |
| 1202 | #ifdef DEBUG_DIAGRAM |
| 1203 | debug_diagram(bts, diag, "poll DL-ACK"); |
| 1204 | if (first_fin_ack) |
| 1205 | debug_diagram(bts, diag, "(is first FINAL)"); |
| 1206 | if (rh->fbi) |
| 1207 | debug_diagram(bts, diag, "(FBI is set)"); |
| 1208 | #endif |
| 1209 | |
| 1210 | /* set polling in header */ |
| 1211 | rh->rrbp = 0; /* N+13 */ |
| 1212 | rh->s_p = 1; /* Polling */ |
| 1213 | |
| 1214 | /* Increment TX-counter */ |
| 1215 | dir.dl.tx_counter++; |
| 1216 | } |
| 1217 | } else { |
| 1218 | /* Increment TX-counter */ |
| 1219 | dir.dl.tx_counter++; |
| 1220 | } |
| 1221 | |
| 1222 | /* return data block as message */ |
| 1223 | dl_msg = msgb_alloc(len, "rlcmac_dl_data"); |
| 1224 | if (!dl_msg) |
| 1225 | return NULL; |
| 1226 | memcpy(msgb_put(dl_msg, len), data, len); |
| 1227 | |
| 1228 | return dl_msg; |
| 1229 | } |
| 1230 | |
Holger Hans Peter Freyther | 2db7e7e | 2013-10-26 20:45:35 +0200 | [diff] [blame] | 1231 | struct msgb *gprs_rlcmac_tbf::create_dl_ass(uint32_t fn) |
| 1232 | { |
| 1233 | struct msgb *msg; |
| 1234 | struct gprs_rlcmac_tbf *new_tbf; |
| 1235 | int poll_ass_dl = POLLING_ASSIGNMENT_DL; |
| 1236 | |
| 1237 | if (poll_ass_dl && direction == GPRS_RLCMAC_DL_TBF |
| 1238 | && control_ts != first_common_ts) { |
| 1239 | LOGP(DRLCMAC, LOGL_NOTICE, "Cannot poll for downlink " |
| 1240 | "assigment, because MS cannot reply. (control TS=%d, " |
| 1241 | "first common TS=%d)\n", control_ts, |
| 1242 | first_common_ts); |
| 1243 | poll_ass_dl = 0; |
| 1244 | } |
| 1245 | if (poll_ass_dl) { |
| 1246 | if (poll_state != GPRS_RLCMAC_POLL_NONE) { |
| 1247 | LOGP(DRLCMAC, LOGL_DEBUG, "Polling is already sheduled " |
| 1248 | "for TBF=%d, so we must wait for downlink " |
| 1249 | "assignment...\n", tfi); |
| 1250 | return NULL; |
| 1251 | } |
| 1252 | if (bts->sba()->find(trx_no, control_ts, (fn + 13) % 2715648)) { |
| 1253 | LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already " |
| 1254 | "scheduled for single block allocation...\n"); |
| 1255 | return NULL; |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | /* on uplink TBF we get the downlink TBF to be assigned. */ |
| 1260 | if (direction == GPRS_RLCMAC_UL_TBF) { |
| 1261 | /* be sure to check first, if contention resolution is done, |
| 1262 | * otherwise we cannot send the assignment yet */ |
| 1263 | if (!dir.ul.contention_resolution_done) { |
| 1264 | LOGP(DRLCMAC, LOGL_DEBUG, "Cannot assign DL TBF now, " |
| 1265 | "because contention resolution is not " |
| 1266 | "finished.\n"); |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | #warning "THIS should probably go over the IMSI too" |
| 1270 | new_tbf = bts->tbf_by_tlli(tlli, GPRS_RLCMAC_DL_TBF); |
| 1271 | } else |
| 1272 | new_tbf = this; |
| 1273 | if (!new_tbf) { |
| 1274 | LOGP(DRLCMACDL, LOGL_ERROR, "We have a schedule for downlink " |
| 1275 | "assignment at uplink TBF=%d, but there is no downlink " |
| 1276 | "TBF\n", tfi); |
| 1277 | dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE; |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | |
| 1281 | msg = msgb_alloc(23, "rlcmac_dl_ass"); |
| 1282 | if (!msg) |
| 1283 | return NULL; |
| 1284 | bitvec *ass_vec = bitvec_alloc(23); |
| 1285 | if (!ass_vec) { |
| 1286 | msgb_free(msg); |
| 1287 | return NULL; |
| 1288 | } |
| 1289 | bitvec_unhex(ass_vec, |
| 1290 | "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); |
| 1291 | LOGP(DRLCMAC, LOGL_INFO, "TBF: START TFI: %u TLLI: 0x%08x Packet Downlink Assignment (PACCH)\n", new_tbf->tfi, new_tbf->tlli); |
| 1292 | RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t); |
| 1293 | Encoding::write_packet_downlink_assignment(mac_control_block, tfi, |
| 1294 | (direction == GPRS_RLCMAC_DL_TBF), new_tbf, |
| 1295 | poll_ass_dl, bts_data()->alpha, bts_data()->gamma, -1, 0); |
| 1296 | LOGP(DRLCMAC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Downlink Assignment +++++++++++++++++++++++++\n"); |
| 1297 | encode_gsm_rlcmac_downlink(ass_vec, mac_control_block); |
| 1298 | LOGPC(DCSN1, LOGL_NOTICE, "\n"); |
| 1299 | LOGP(DRLCMAC, LOGL_DEBUG, "------------------------- TX : Packet Downlink Assignment -------------------------\n"); |
| 1300 | bitvec_pack(ass_vec, msgb_put(msg, 23)); |
| 1301 | bitvec_free(ass_vec); |
| 1302 | talloc_free(mac_control_block); |
| 1303 | |
| 1304 | if (poll_ass_dl) { |
| 1305 | poll_state = GPRS_RLCMAC_POLL_SCHED; |
| 1306 | poll_fn = (fn + 13) % 2715648; |
| 1307 | dl_ass_state = GPRS_RLCMAC_DL_ASS_WAIT_ACK; |
| 1308 | } else { |
| 1309 | dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE; |
| 1310 | tbf_new_state(new_tbf, GPRS_RLCMAC_FLOW); |
| 1311 | tbf_assign_control_ts(new_tbf); |
| 1312 | /* stop pending assignment timer */ |
| 1313 | new_tbf->stop_timer(); |
| 1314 | |
| 1315 | } |
| 1316 | debug_diagram(bts, diag, "send DL-ASS"); |
| 1317 | |
| 1318 | return msg; |
| 1319 | } |
| 1320 | |
Holger Hans Peter Freyther | ae03f22 | 2013-10-26 21:20:51 +0200 | [diff] [blame] | 1321 | struct msgb *gprs_rlcmac_tbf::create_ul_ass(uint32_t fn) |
| 1322 | { |
| 1323 | struct msgb *msg; |
| 1324 | struct gprs_rlcmac_tbf *new_tbf; |
| 1325 | |
| 1326 | #if POLLING_ASSIGNMENT_UL == 1 |
| 1327 | if (poll_state != GPRS_RLCMAC_POLL_NONE) { |
| 1328 | LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already " |
| 1329 | "sheduled for TBF=%d, so we must wait for uplink " |
| 1330 | "assignment...\n", tfi); |
| 1331 | return NULL; |
| 1332 | } |
| 1333 | if (bts->sba()->find(trx_no, control_ts, (fn + 13) % 2715648)) { |
| 1334 | LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already scheduled for " |
| 1335 | "single block allocation...\n"); |
| 1336 | return NULL; |
| 1337 | } |
| 1338 | #endif |
| 1339 | |
| 1340 | /* on down TBF we get the uplink TBF to be assigned. */ |
| 1341 | #warning "Probably want to find by IMSI too" |
| 1342 | if (direction == GPRS_RLCMAC_DL_TBF) |
| 1343 | new_tbf = bts->tbf_by_tlli(tlli, GPRS_RLCMAC_UL_TBF); |
| 1344 | else |
| 1345 | new_tbf = this; |
| 1346 | |
| 1347 | if (!new_tbf) { |
| 1348 | LOGP(DRLCMACUL, LOGL_ERROR, "We have a schedule for uplink " |
| 1349 | "assignment at downlink TBF=%d, but there is no uplink " |
| 1350 | "TBF\n", tfi); |
| 1351 | ul_ass_state = GPRS_RLCMAC_UL_ASS_NONE; |
| 1352 | return NULL; |
| 1353 | } |
| 1354 | |
| 1355 | msg = msgb_alloc(23, "rlcmac_ul_ass"); |
| 1356 | if (!msg) |
| 1357 | return NULL; |
| 1358 | LOGP(DRLCMAC, LOGL_INFO, "TBF: START TFI: %u TLLI: 0x%08x Packet Uplink Assignment (PACCH)\n", new_tbf->tfi, new_tbf->tlli); |
| 1359 | bitvec *ass_vec = bitvec_alloc(23); |
| 1360 | if (!ass_vec) { |
| 1361 | msgb_free(msg); |
| 1362 | return NULL; |
| 1363 | } |
| 1364 | bitvec_unhex(ass_vec, |
| 1365 | "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); |
| 1366 | Encoding::write_packet_uplink_assignment(bts_data(), ass_vec, tfi, |
| 1367 | (direction == GPRS_RLCMAC_DL_TBF), tlli, |
| 1368 | tlli_valid, new_tbf, POLLING_ASSIGNMENT_UL, bts_data()->alpha, |
| 1369 | bts_data()->gamma, -1); |
| 1370 | bitvec_pack(ass_vec, msgb_put(msg, 23)); |
| 1371 | RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t); |
| 1372 | LOGP(DRLCMAC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Uplink Assignment +++++++++++++++++++++++++\n"); |
| 1373 | decode_gsm_rlcmac_downlink(ass_vec, mac_control_block); |
| 1374 | LOGPC(DCSN1, LOGL_NOTICE, "\n"); |
| 1375 | LOGP(DRLCMAC, LOGL_DEBUG, "------------------------- TX : Packet Uplink Assignment -------------------------\n"); |
| 1376 | bitvec_free(ass_vec); |
| 1377 | talloc_free(mac_control_block); |
| 1378 | |
| 1379 | #if POLLING_ASSIGNMENT_UL == 1 |
| 1380 | poll_state = GPRS_RLCMAC_POLL_SCHED; |
| 1381 | poll_fn = (fn + 13) % 2715648; |
| 1382 | ul_ass_state = GPRS_RLCMAC_UL_ASS_WAIT_ACK; |
| 1383 | #else |
| 1384 | ul_ass_state = GPRS_RLCMAC_UL_ASS_NONE; |
| 1385 | tbf_new_state(new_tbf, GPRS_RLCMAC_FLOW); |
| 1386 | tbf_assign_control_ts(new_tbf); |
| 1387 | #endif |
| 1388 | debug_diagram(bts, diag, "send UL-ASS"); |
| 1389 | |
| 1390 | return msg; |
| 1391 | } |
| 1392 | |
Holger Hans Peter Freyther | 46bcb8d | 2013-10-26 21:04:28 +0200 | [diff] [blame] | 1393 | struct msgb *gprs_rlcmac_tbf::create_ul_ack(uint32_t fn) |
| 1394 | { |
| 1395 | int final = (state_is(GPRS_RLCMAC_FINISHED)); |
| 1396 | struct msgb *msg; |
| 1397 | |
| 1398 | if (final) { |
| 1399 | if (poll_state != GPRS_RLCMAC_POLL_NONE) { |
| 1400 | LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already " |
| 1401 | "sheduled for TBF=%d, so we must wait for " |
| 1402 | "final uplink ack...\n", tfi); |
| 1403 | return NULL; |
| 1404 | } |
| 1405 | if (bts->sba()->find(trx_no, control_ts, (fn + 13) % 2715648)) { |
| 1406 | LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already " |
| 1407 | "scheduled for single block allocation...\n"); |
| 1408 | return NULL; |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | msg = msgb_alloc(23, "rlcmac_ul_ack"); |
| 1413 | if (!msg) |
| 1414 | return NULL; |
| 1415 | bitvec *ack_vec = bitvec_alloc(23); |
| 1416 | if (!ack_vec) { |
| 1417 | msgb_free(msg); |
| 1418 | return NULL; |
| 1419 | } |
| 1420 | bitvec_unhex(ack_vec, |
| 1421 | "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); |
| 1422 | RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t); |
| 1423 | Encoding::write_packet_uplink_ack(bts_data(), mac_control_block, this, final); |
| 1424 | encode_gsm_rlcmac_downlink(ack_vec, mac_control_block); |
| 1425 | bitvec_pack(ack_vec, msgb_put(msg, 23)); |
| 1426 | bitvec_free(ack_vec); |
| 1427 | talloc_free(mac_control_block); |
| 1428 | |
| 1429 | /* now we must set this flag, so we are allowed to assign downlink |
Holger Hans Peter Freyther | 22b3119 | 2013-10-26 21:07:45 +0200 | [diff] [blame] | 1430 | * TBF on PACCH. it is only allowed when TLLI is acknowledged. */ |
Holger Hans Peter Freyther | 46bcb8d | 2013-10-26 21:04:28 +0200 | [diff] [blame] | 1431 | dir.ul.contention_resolution_done = 1; |
| 1432 | |
| 1433 | if (final) { |
| 1434 | poll_state = GPRS_RLCMAC_POLL_SCHED; |
| 1435 | poll_fn = (fn + 13) % 2715648; |
| 1436 | /* waiting for final acknowledge */ |
| 1437 | ul_ack_state = GPRS_RLCMAC_UL_ACK_WAIT_ACK; |
| 1438 | dir.ul.final_ack_sent = 1; |
| 1439 | } else |
| 1440 | ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE; |
| 1441 | debug_diagram(bts->bts, diag, "send UL-ACK"); |
| 1442 | |
| 1443 | return msg; |
| 1444 | } |
| 1445 | |
Holger Hans Peter Freyther | 3dc56a3 | 2013-10-26 21:38:30 +0200 | [diff] [blame] | 1446 | int gprs_rlcmac_tbf::snd_dl_ack(uint8_t final, uint8_t ssn, uint8_t *rbb) |
| 1447 | { |
| 1448 | char show_rbb[65], show_v_b[RLC_MAX_SNS + 1]; |
| 1449 | uint16_t mod_sns = sns - 1; |
| 1450 | uint16_t mod_sns_half = (sns >> 1) - 1; |
| 1451 | int i; /* must be signed */ |
| 1452 | int16_t dist; /* must be signed */ |
| 1453 | uint8_t bit; |
| 1454 | uint16_t bsn; |
| 1455 | struct msgb *msg; |
| 1456 | uint16_t lost = 0, received = 0; |
| 1457 | |
| 1458 | LOGP(DRLCMACDL, LOGL_DEBUG, "TBF=%d downlink acknowledge\n", tfi); |
| 1459 | |
| 1460 | if (!final) { |
| 1461 | /* show received array in debug (bit 64..1) */ |
| 1462 | for (i = 63; i >= 0; i--) { |
| 1463 | bit = (rbb[i >> 3] >> (7 - (i&7))) & 1; |
| 1464 | show_rbb[i] = (bit) ? '1' : 'o'; |
| 1465 | } |
| 1466 | show_rbb[64] = '\0'; |
| 1467 | LOGP(DRLCMACDL, LOGL_DEBUG, "- ack: (BSN=%d)\"%s\"" |
| 1468 | "(BSN=%d) 1=ACK o=NACK\n", (ssn - 64) & mod_sns, |
| 1469 | show_rbb, (ssn - 1) & mod_sns); |
| 1470 | |
| 1471 | /* apply received array to receive state (SSN-64..SSN-1) */ |
| 1472 | /* calculate distance of ssn from V(S) */ |
| 1473 | dist = (dir.dl.v_s - ssn) & mod_sns; |
| 1474 | /* check if distance is less than distance V(A)..V(S) */ |
| 1475 | if (dist >= ((dir.dl.v_s - dir.dl.v_a) & mod_sns)) { |
| 1476 | /* this might happpen, if the downlink assignment |
| 1477 | * was not received by ms and the ack refers |
| 1478 | * to previous TBF |
| 1479 | * FIXME: we should implement polling for |
| 1480 | * control ack!*/ |
| 1481 | LOGP(DRLCMACDL, LOGL_NOTICE, "- ack range is out of " |
| 1482 | "V(A)..V(S) range (DL TBF=%d) Free TFB!\n", tfi); |
| 1483 | return 1; /* indicate to free TBF */ |
| 1484 | } |
| 1485 | /* SSN - 1 is in range V(A)..V(S)-1 */ |
| 1486 | for (i = 63, bsn = (ssn - 1) & mod_sns; |
| 1487 | i >= 0 && bsn != ((dir.dl.v_a - 1) & mod_sns); |
| 1488 | i--, bsn = (bsn - 1) & mod_sns) { |
| 1489 | bit = (rbb[i >> 3] >> (7 - (i&7))) & 1; |
| 1490 | if (bit) { |
| 1491 | LOGP(DRLCMACDL, LOGL_DEBUG, "- got " |
| 1492 | "ack for BSN=%d\n", bsn); |
| 1493 | if (dir.dl.v_b[bsn & mod_sns_half] |
| 1494 | != 'A') |
| 1495 | received++; |
| 1496 | dir.dl.v_b[bsn & mod_sns_half] = 'A'; |
| 1497 | } else { |
| 1498 | LOGP(DRLCMACDL, LOGL_DEBUG, "- got " |
| 1499 | "NACK for BSN=%d\n", bsn); |
| 1500 | dir.dl.v_b[bsn & mod_sns_half] = 'N'; |
| 1501 | lost++; |
| 1502 | } |
| 1503 | } |
| 1504 | /* report lost and received packets */ |
| 1505 | gprs_rlcmac_received_lost(this, received, lost); |
| 1506 | |
| 1507 | /* raise V(A), if possible */ |
| 1508 | for (i = 0, bsn = dir.dl.v_a; bsn != dir.dl.v_s; |
| 1509 | i++, bsn = (bsn + 1) & mod_sns) { |
| 1510 | if (dir.dl.v_b[bsn & mod_sns_half] == 'A') { |
| 1511 | dir.dl.v_b[bsn & mod_sns_half] = 'I'; |
| 1512 | /* mark invalid */ |
| 1513 | dir.dl.v_a = (dir.dl.v_a + 1) |
| 1514 | & mod_sns; |
| 1515 | } else |
| 1516 | break; |
| 1517 | } |
| 1518 | |
| 1519 | /* show receive state array in debug (V(A)..V(S)-1) */ |
| 1520 | for (i = 0, bsn = dir.dl.v_a; bsn != dir.dl.v_s; |
| 1521 | i++, bsn = (bsn + 1) & mod_sns) { |
| 1522 | show_v_b[i] = dir.dl.v_b[bsn & mod_sns_half]; |
| 1523 | if (show_v_b[i] == 0) |
| 1524 | show_v_b[i] = ' '; |
| 1525 | } |
| 1526 | show_v_b[i] = '\0'; |
| 1527 | LOGP(DRLCMACDL, LOGL_DEBUG, "- V(B): (V(A)=%d)\"%s\"" |
| 1528 | "(V(S)-1=%d) A=Acked N=Nacked U=Unacked " |
| 1529 | "X=Resend-Unacked\n", dir.dl.v_a, show_v_b, |
| 1530 | (dir.dl.v_s - 1) & mod_sns); |
| 1531 | |
| 1532 | if (state_is(GPRS_RLCMAC_FINISHED) |
| 1533 | && dir.dl.v_s == dir.dl.v_a) { |
| 1534 | LOGP(DRLCMACDL, LOGL_NOTICE, "Received acknowledge of " |
| 1535 | "all blocks, but without final ack " |
| 1536 | "inidcation (don't worry)\n"); |
| 1537 | } |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
| 1541 | LOGP(DRLCMACDL, LOGL_DEBUG, "- Final ACK received.\n"); |
| 1542 | debug_diagram(ts, diag, "got Final ACK"); |
| 1543 | /* range V(A)..V(S)-1 */ |
| 1544 | for (bsn = dir.dl.v_a; bsn != dir.dl.v_s; |
| 1545 | bsn = (bsn + 1) & mod_sns) { |
| 1546 | if (dir.dl.v_b[bsn & mod_sns_half] != 'A') |
| 1547 | received++; |
| 1548 | } |
| 1549 | |
| 1550 | /* report all outstanding packets as received */ |
| 1551 | gprs_rlcmac_received_lost(this, received, lost); |
| 1552 | |
| 1553 | /* check for LLC PDU in the LLC Queue */ |
| 1554 | msg = llc_dequeue(gprs_bssgp_pcu_current_bctx()); |
| 1555 | if (!msg) { |
| 1556 | /* no message, start T3193, change state to RELEASE */ |
| 1557 | LOGP(DRLCMACDL, LOGL_DEBUG, "- No new message, so we " |
| 1558 | "release.\n"); |
| 1559 | /* start T3193 */ |
| 1560 | debug_diagram(bts, diag, "start T3193"); |
| 1561 | tbf_timer_start(this, 3193, |
| 1562 | bts_data()->t3193_msec / 1000, |
| 1563 | (bts_data()->t3193_msec % 1000) * 1000); |
| 1564 | tbf_new_state(this, GPRS_RLCMAC_WAIT_RELEASE); |
| 1565 | |
| 1566 | return 0; |
| 1567 | } |
| 1568 | #warning "Copy and paste on the sender path" |
| 1569 | update_llc_frame(msg); |
| 1570 | msgb_free(msg); |
| 1571 | |
| 1572 | /* we have a message, so we trigger downlink assignment, and there |
| 1573 | * set the state to ASSIGN. also we set old_downlink, because we |
| 1574 | * re-use this tbf. */ |
| 1575 | LOGP(DRLCMAC, LOGL_DEBUG, "Trigger dowlink assignment on PACCH, " |
| 1576 | "because another LLC PDU has arrived in between\n"); |
| 1577 | memset(&dir.dl, 0, sizeof(dir.dl)); /* reset RLC states */ |
| 1578 | state_flags &= GPRS_RLCMAC_FLAG_TO_MASK; /* keep TO flags */ |
| 1579 | state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH); |
| 1580 | update(); |
| 1581 | bts->trigger_dl_ass(this, this, NULL); |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
Holger Hans Peter Freyther | 964ddb6 | 2013-10-16 17:53:23 +0200 | [diff] [blame] | 1585 | void gprs_rlcmac_tbf::free_all(struct gprs_rlcmac_trx *trx) |
| 1586 | { |
| 1587 | for (uint8_t tfi = 0; tfi < 32; tfi++) { |
| 1588 | struct gprs_rlcmac_tbf *tbf; |
| 1589 | |
| 1590 | tbf = trx->ul_tbf[tfi]; |
| 1591 | if (tbf) |
| 1592 | tbf_free(tbf); |
| 1593 | tbf = trx->dl_tbf[tfi]; |
| 1594 | if (tbf) |
| 1595 | tbf_free(tbf); |
| 1596 | } |
| 1597 | } |
Holger Hans Peter Freyther | 4f6a4e5d | 2013-10-16 17:58:46 +0200 | [diff] [blame] | 1598 | |
| 1599 | void gprs_rlcmac_tbf::free_all(struct gprs_rlcmac_pdch *pdch) |
| 1600 | { |
| 1601 | for (uint8_t tfi = 0; tfi < 32; tfi++) { |
| 1602 | struct gprs_rlcmac_tbf *tbf; |
| 1603 | |
| 1604 | tbf = pdch->ul_tbf[tfi]; |
| 1605 | if (tbf) |
| 1606 | tbf_free(tbf); |
| 1607 | tbf = pdch->dl_tbf[tfi]; |
| 1608 | if (tbf) |
| 1609 | tbf_free(tbf); |
| 1610 | } |
| 1611 | } |