Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 by Holger Hans Peter Freyther |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
Holger Hans Peter Freyther | 099535a | 2013-10-16 17:42:31 +0200 | [diff] [blame] | 21 | #include "gprs_rlcmac.h" |
| 22 | |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | |
Holger Hans Peter Freyther | 9e21d84 | 2013-10-16 17:48:12 +0200 | [diff] [blame] | 25 | /* |
| 26 | * TBF instance |
| 27 | */ |
| 28 | |
| 29 | #define LLC_MAX_LEN 1543 |
| 30 | #define RLC_MAX_SNS 128 /* GPRS, must be power of 2 */ |
| 31 | #define RLC_MAX_WS 64 /* max window size */ |
| 32 | #define RLC_MAX_LEN 54 /* CS-4 including spare bits */ |
| 33 | |
| 34 | #define Tassign_agch 0,200000 /* waiting after IMM.ASS confirm */ |
| 35 | #define Tassign_pacch 2,0 /* timeout for pacch assigment */ |
| 36 | |
| 37 | enum gprs_rlcmac_tbf_state { |
| 38 | GPRS_RLCMAC_NULL = 0, /* new created TBF */ |
| 39 | GPRS_RLCMAC_ASSIGN, /* wait for downlink assignment */ |
| 40 | GPRS_RLCMAC_FLOW, /* RLC/MAC flow, resource needed */ |
| 41 | GPRS_RLCMAC_FINISHED, /* flow finished, wait for release */ |
| 42 | GPRS_RLCMAC_WAIT_RELEASE,/* wait for release or restart of DL TBF */ |
| 43 | GPRS_RLCMAC_RELEASING, /* releasing, wait to free TBI/USF */ |
| 44 | }; |
| 45 | |
| 46 | enum gprs_rlcmac_tbf_poll_state { |
| 47 | GPRS_RLCMAC_POLL_NONE = 0, |
| 48 | GPRS_RLCMAC_POLL_SCHED, /* a polling was scheduled */ |
| 49 | }; |
| 50 | |
| 51 | enum gprs_rlcmac_tbf_dl_ass_state { |
| 52 | GPRS_RLCMAC_DL_ASS_NONE = 0, |
| 53 | GPRS_RLCMAC_DL_ASS_SEND_ASS, /* send downlink assignment on next RTS */ |
| 54 | GPRS_RLCMAC_DL_ASS_WAIT_ACK, /* wait for PACKET CONTROL ACK */ |
| 55 | }; |
| 56 | |
| 57 | enum gprs_rlcmac_tbf_ul_ass_state { |
| 58 | GPRS_RLCMAC_UL_ASS_NONE = 0, |
| 59 | GPRS_RLCMAC_UL_ASS_SEND_ASS, /* send uplink assignment on next RTS */ |
| 60 | GPRS_RLCMAC_UL_ASS_WAIT_ACK, /* wait for PACKET CONTROL ACK */ |
| 61 | }; |
| 62 | |
| 63 | enum gprs_rlcmac_tbf_ul_ack_state { |
| 64 | GPRS_RLCMAC_UL_ACK_NONE = 0, |
| 65 | GPRS_RLCMAC_UL_ACK_SEND_ACK, /* send acknowledge on next RTS */ |
| 66 | GPRS_RLCMAC_UL_ACK_WAIT_ACK, /* wait for PACKET CONTROL ACK */ |
| 67 | }; |
| 68 | |
| 69 | enum gprs_rlcmac_tbf_direction { |
| 70 | GPRS_RLCMAC_DL_TBF, |
| 71 | GPRS_RLCMAC_UL_TBF |
| 72 | }; |
| 73 | |
| 74 | #define GPRS_RLCMAC_FLAG_CCCH 0 /* assignment on CCCH */ |
| 75 | #define GPRS_RLCMAC_FLAG_PACCH 1 /* assignment on PACCH */ |
| 76 | #define GPRS_RLCMAC_FLAG_UL_DATA 2 /* uplink data received */ |
| 77 | #define GPRS_RLCMAC_FLAG_DL_ACK 3 /* downlink acknowledge received */ |
| 78 | #define GPRS_RLCMAC_FLAG_TO_UL_ACK 4 |
| 79 | #define GPRS_RLCMAC_FLAG_TO_DL_ACK 5 |
| 80 | #define GPRS_RLCMAC_FLAG_TO_UL_ASS 6 |
| 81 | #define GPRS_RLCMAC_FLAG_TO_DL_ASS 7 |
| 82 | #define GPRS_RLCMAC_FLAG_TO_MASK 0xf0 /* timeout bits */ |
| 83 | |
| 84 | extern struct llist_head gprs_rlcmac_ul_tbfs; /* list of uplink TBFs */ |
| 85 | extern struct llist_head gprs_rlcmac_dl_tbfs; /* list of downlink TBFs */ |
| 86 | extern struct llist_head gprs_rlcmac_sbas; /* list of single block allocs */ |
| 87 | |
| 88 | |
Holger Hans Peter Freyther | 099535a | 2013-10-16 17:42:31 +0200 | [diff] [blame] | 89 | struct gprs_rlcmac_tbf { |
Holger Hans Peter Freyther | 964ddb6 | 2013-10-16 17:53:23 +0200 | [diff] [blame] | 90 | |
| 91 | static void free_all(struct gprs_rlcmac_trx *trx); |
Holger Hans Peter Freyther | 4f6a4e5d | 2013-10-16 17:58:46 +0200 | [diff] [blame] | 92 | static void free_all(struct gprs_rlcmac_pdch *pdch); |
Holger Hans Peter Freyther | 964ddb6 | 2013-10-16 17:53:23 +0200 | [diff] [blame] | 93 | |
Holger Hans Peter Freyther | 1c344e2 | 2013-10-16 18:33:18 +0200 | [diff] [blame] | 94 | bool state_is(enum gprs_rlcmac_tbf_state rhs) const; |
| 95 | bool state_is_not(enum gprs_rlcmac_tbf_state rhs) const; |
| 96 | void set_state(enum gprs_rlcmac_tbf_state new_state); |
| 97 | |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 98 | int rlcmac_diag(); |
Holger Hans Peter Freyther | 964ddb6 | 2013-10-16 17:53:23 +0200 | [diff] [blame] | 99 | |
Holger Hans Peter Freyther | 099535a | 2013-10-16 17:42:31 +0200 | [diff] [blame] | 100 | struct llist_head list; |
Holger Hans Peter Freyther | 099535a | 2013-10-16 17:42:31 +0200 | [diff] [blame] | 101 | uint32_t state_flags; |
| 102 | enum gprs_rlcmac_tbf_direction direction; |
| 103 | uint8_t tfi; |
| 104 | uint32_t tlli; |
| 105 | uint8_t tlli_valid; |
Holger Hans Peter Freyther | 743bafa | 2013-09-29 07:50:50 +0200 | [diff] [blame] | 106 | struct gprs_rlcmac_trx *trx; |
Holger Hans Peter Freyther | 96efa70 | 2013-09-29 07:44:39 +0200 | [diff] [blame] | 107 | uint8_t trx_no; |
Holger Hans Peter Freyther | 099535a | 2013-10-16 17:42:31 +0200 | [diff] [blame] | 108 | uint16_t arfcn; |
| 109 | uint8_t tsc; |
| 110 | uint8_t first_ts; /* first TS used by TBF */ |
| 111 | uint8_t first_common_ts; /* first TS that the phone can send and |
| 112 | reveive simultaniously */ |
| 113 | uint8_t control_ts; /* timeslot control messages and polling */ |
| 114 | uint8_t ms_class; |
| 115 | struct gprs_rlcmac_pdch *pdch[8]; /* list of PDCHs allocated to TBF */ |
| 116 | uint16_t ta; |
| 117 | uint8_t llc_frame[LLC_MAX_LEN]; /* current DL or UL frame */ |
| 118 | uint16_t llc_index; /* current write/read position of frame */ |
| 119 | uint16_t llc_length; /* len of current DL LLC_frame, 0 == no frame */ |
| 120 | struct llist_head llc_queue; /* queued LLC DL data */ |
| 121 | |
| 122 | enum gprs_rlcmac_tbf_dl_ass_state dl_ass_state; |
| 123 | enum gprs_rlcmac_tbf_ul_ass_state ul_ass_state; |
| 124 | enum gprs_rlcmac_tbf_ul_ack_state ul_ack_state; |
| 125 | |
| 126 | enum gprs_rlcmac_tbf_poll_state poll_state; |
| 127 | uint32_t poll_fn; /* frame number to poll */ |
| 128 | |
| 129 | uint16_t ws; /* window size */ |
| 130 | uint16_t sns; /* sequence number space */ |
| 131 | |
| 132 | /* Please note that all variables here will be reset when changing |
| 133 | * from WAIT RELEASE back to FLOW state (re-use of TBF). |
| 134 | * All states that need reset must be in this struct, so this is why |
| 135 | * variables are in both (dl and ul) structs and not outside union. |
| 136 | */ |
| 137 | union { |
| 138 | struct { |
| 139 | uint16_t bsn; /* block sequence number */ |
| 140 | uint16_t v_s; /* send state */ |
| 141 | uint16_t v_a; /* ack state */ |
| 142 | char v_b[RLC_MAX_SNS/2]; /* acknowledge state array */ |
| 143 | int32_t tx_counter; /* count all transmitted blocks */ |
| 144 | char imsi[16]; /* store IMSI for PCH retransmission */ |
| 145 | uint8_t wait_confirm; /* wait for CCCH IMM.ASS cnf */ |
| 146 | } dl; |
| 147 | struct { |
| 148 | uint16_t bsn; /* block sequence number */ |
| 149 | uint16_t v_r; /* receive state */ |
| 150 | uint16_t v_q; /* receive window state */ |
| 151 | char v_n[RLC_MAX_SNS/2]; /* receive state array */ |
| 152 | int32_t rx_counter; /* count all received blocks */ |
| 153 | uint8_t n3103; /* N3103 counter */ |
| 154 | uint8_t usf[8]; /* list USFs per PDCH (timeslot) */ |
| 155 | uint8_t contention_resolution_done; /* set after done */ |
| 156 | uint8_t final_ack_sent; /* set if we sent final ack */ |
| 157 | } ul; |
| 158 | } dir; |
| 159 | uint8_t rlc_block[RLC_MAX_SNS/2][RLC_MAX_LEN]; /* block history */ |
| 160 | uint8_t rlc_block_len[RLC_MAX_SNS/2]; /* block len of history */ |
| 161 | |
| 162 | uint8_t n3105; /* N3105 counter */ |
| 163 | |
| 164 | struct osmo_timer_list timer; |
| 165 | unsigned int T; /* Txxxx number */ |
| 166 | unsigned int num_T_exp; /* number of consecutive T expirations */ |
| 167 | |
| 168 | struct osmo_gsm_timer_list gsm_timer; |
| 169 | unsigned int fT; /* fTxxxx number */ |
| 170 | unsigned int num_fT_exp; /* number of consecutive fT expirations */ |
| 171 | |
| 172 | struct { |
| 173 | char imsi[16]; |
| 174 | |
| 175 | struct timeval dl_bw_tv; /* timestamp for dl bw calculation */ |
| 176 | uint32_t dl_bw_octets; /* number of octets since bw_tv */ |
| 177 | |
| 178 | struct timeval rssi_tv; /* timestamp for rssi calculation */ |
| 179 | int32_t rssi_sum; /* sum of rssi values */ |
| 180 | int rssi_num; /* number of rssi values added since rssi_tv */ |
| 181 | |
| 182 | struct timeval dl_loss_tv; /* timestamp for loss calculation */ |
| 183 | uint16_t dl_loss_lost; /* sum of lost packets */ |
| 184 | uint16_t dl_loss_received; /* sum of received packets */ |
| 185 | |
| 186 | } meas; |
| 187 | |
| 188 | uint8_t cs; /* current coding scheme */ |
| 189 | |
| 190 | #ifdef DEBUG_DIAGRAM |
| 191 | int diag; /* number where TBF is presented in diagram */ |
| 192 | int diag_new; /* used to format output of new TBF */ |
| 193 | #endif |
Holger Hans Peter Freyther | 1c344e2 | 2013-10-16 18:33:18 +0200 | [diff] [blame] | 194 | |
| 195 | /* these should become protected but only after gprs_rlcmac_data.c |
| 196 | * stops to iterate over all tbf in its current form */ |
| 197 | enum gprs_rlcmac_tbf_state state; |
Holger Hans Peter Freyther | 099535a | 2013-10-16 17:42:31 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 200 | |
Holger Hans Peter Freyther | 8692128 | 2013-08-24 21:26:42 +0200 | [diff] [blame] | 201 | /* dispatch Unitdata.DL messages */ |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 202 | int tbf_handle(struct gprs_rlcmac_bts *bts, |
| 203 | const uint32_t tlli, const char *imsi, const uint8_t ms_class, |
| 204 | const uint16_t delay_csec, const uint8_t *data, const uint16_t len); |
Holger Hans Peter Freyther | 8692128 | 2013-08-24 21:26:42 +0200 | [diff] [blame] | 205 | |
| 206 | struct gprs_rlcmac_tbf *tbf_alloc_ul(struct gprs_rlcmac_bts *bts, |
| 207 | int8_t use_trx, uint8_t ms_class, |
| 208 | uint32_t tlli, uint8_t ta, struct gprs_rlcmac_tbf *dl_tbf); |
Holger Hans Peter Freyther | 9e21d84 | 2013-10-16 17:48:12 +0200 | [diff] [blame] | 209 | |
| 210 | int tfi_find_free(struct gprs_rlcmac_bts *bts, enum gprs_rlcmac_tbf_direction dir, |
| 211 | uint8_t *_trx, int8_t use_trx); |
| 212 | |
| 213 | struct gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts, |
| 214 | struct gprs_rlcmac_tbf *old_tbf, |
| 215 | enum gprs_rlcmac_tbf_direction dir, uint8_t tfi, uint8_t trx, |
| 216 | uint8_t ms_class, uint8_t single_slot); |
| 217 | |
| 218 | struct gprs_rlcmac_tbf *tbf_by_tfi(struct gprs_rlcmac_bts *bts, |
| 219 | uint8_t tfi, uint8_t trx, |
| 220 | enum gprs_rlcmac_tbf_direction dir); |
| 221 | |
| 222 | struct gprs_rlcmac_tbf *tbf_by_tlli(uint32_t tlli, |
| 223 | enum gprs_rlcmac_tbf_direction dir); |
| 224 | |
| 225 | struct gprs_rlcmac_tbf *tbf_by_poll_fn(uint32_t fn, uint8_t trx, uint8_t ts); |
| 226 | |
| 227 | void tbf_free(struct gprs_rlcmac_tbf *tbf); |
| 228 | |
| 229 | int tbf_update(struct gprs_rlcmac_tbf *tbf); |
| 230 | |
| 231 | int tbf_assign_control_ts(struct gprs_rlcmac_tbf *tbf); |
| 232 | |
| 233 | void tbf_new_state(struct gprs_rlcmac_tbf *tbf, |
| 234 | enum gprs_rlcmac_tbf_state state); |
| 235 | |
| 236 | void tbf_timer_start(struct gprs_rlcmac_tbf *tbf, unsigned int T, |
| 237 | unsigned int seconds, unsigned int microseconds); |
| 238 | |
| 239 | void tbf_timer_stop(struct gprs_rlcmac_tbf *tbf); |
| 240 | |
Holger Hans Peter Freyther | 7380bab | 2013-10-16 18:09:19 +0200 | [diff] [blame] | 241 | void tbf_timer_cb(void *_tbf); |
| 242 | |
Holger Hans Peter Freyther | 1c344e2 | 2013-10-16 18:33:18 +0200 | [diff] [blame] | 243 | |
| 244 | inline bool gprs_rlcmac_tbf::state_is(enum gprs_rlcmac_tbf_state rhs) const |
| 245 | { |
| 246 | return state == rhs; |
| 247 | } |
| 248 | |
| 249 | inline bool gprs_rlcmac_tbf::state_is_not(enum gprs_rlcmac_tbf_state rhs) const |
| 250 | { |
| 251 | return state != rhs; |
| 252 | } |
| 253 | |
| 254 | inline void gprs_rlcmac_tbf::set_state(enum gprs_rlcmac_tbf_state new_state) |
| 255 | { |
| 256 | state = new_state; |
| 257 | } |