blob: 89b3dc96790aa5143d140f395d9e9db5f03c527d [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* gprs_rlcmac.h
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#ifndef GPRS_RLCMAC_H
21#define GPRS_RLCMAC_H
22
Andreas Eversberg8b761a32012-07-20 21:50:31 +020023#ifdef __cplusplus
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040024#include <gsm_rlcmac.h>
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +040025#include <gsm_timer.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040026
27extern "C" {
28#include <osmocom/core/linuxlist.h>
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +040029#include <osmocom/core/timer.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040030}
Andreas Eversberg8b761a32012-07-20 21:50:31 +020031#endif
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040032
Andreas Eversberge6228b32012-07-03 13:36:03 +020033/* This special feature will delay assignment of downlink TBF by one second,
34 * in case there is already a TBF.
35 * This is usefull to debug downlink establishment during packet idle mode.
36 */
37//#define DEBUG_DL_ASS_IDLE
Ivan Kluchnikovc5d8c272012-06-04 21:55:02 +040038
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020039/*
40 * PDCH instanc
41 */
42
43struct gprs_rlcmac_tbf;
44
45struct gprs_rlcmac_pdch {
46 uint8_t enable; /* TS is enabled */
47 uint8_t tsc; /* TSC of this slot */
48 uint8_t next_ul_tfi; /* next uplink TBF/TFI to schedule (0..31) */
49 uint8_t next_dl_tfi; /* next downlink TBF/TFI to schedule (0..31) */
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +020050 struct gprs_rlcmac_tbf *ul_tbf[32]; /* array of UL TBF, by UL TFI */
51 struct gprs_rlcmac_tbf *dl_tbf[32]; /* array of DL TBF, by DL TFI */
Andreas Eversberg2b914642012-07-19 13:06:26 +020052 struct llist_head paging_list; /* list of paging messages */
Andreas Eversberge6228b32012-07-03 13:36:03 +020053 uint32_t last_rts_fn; /* store last frame number of RTS */
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +040054};
55
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020056struct gprs_rlcmac_trx {
57 uint16_t arfcn;
58 struct gprs_rlcmac_pdch pdch[8];
59};
60
61struct gprs_rlcmac_bts {
Andreas Eversbergb3c6f6c2012-07-06 07:40:08 +020062 uint8_t cs1;
63 uint8_t cs2;
64 uint8_t cs3;
65 uint8_t cs4;
66 uint8_t initial_cs;
Andreas Eversberg8b761a32012-07-20 21:50:31 +020067 uint8_t force_cs; /* 0=use from BTS 1=use from VTY */
Andreas Eversberg24131bf2012-07-21 11:09:58 +020068 uint16_t force_llc_lifetime; /* overrides lifetime from SGSN */
Andreas Eversbergb3c6f6c2012-07-06 07:40:08 +020069 uint8_t t3142;
70 uint8_t t3169;
71 uint8_t t3191;
72 uint16_t t3193_msec;
73 uint8_t t3195;
74 uint8_t n3101;
75 uint8_t n3103;
76 uint8_t n3105;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020077 struct gprs_rlcmac_trx trx[8];
Andreas Eversberg592e04a2012-07-15 06:25:37 +020078 int (*alloc_algorithm)(struct gprs_rlcmac_tbf *old_tbf,
79 struct gprs_rlcmac_tbf *tbf, uint32_t cust);
80 uint32_t alloc_algorithm_curst; /* options to customize algorithm */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020081};
82
83extern struct gprs_rlcmac_bts *gprs_rlcmac_bts;
84
Andreas Eversberg8b761a32012-07-20 21:50:31 +020085#ifdef __cplusplus
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020086/*
87 * TBF instance
88 */
89
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040090#define LLC_MAX_LEN 1543
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020091#define RLC_MAX_SNS 128 /* GPRS, must be power of 2 */
92#define RLC_MAX_WS 64 /* max window size */
Andreas Eversberge6228b32012-07-03 13:36:03 +020093#define RLC_MAX_LEN 54 /* CS-4 including spare bits */
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040094
Andreas Eversberg642c7d32012-07-23 17:58:55 +020095#define Tassign_agch 0,800000/* FIXME: we need a confirm from BTS */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020096
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040097enum gprs_rlcmac_tbf_state {
Andreas Eversberge6228b32012-07-03 13:36:03 +020098 GPRS_RLCMAC_NULL = 0, /* new created TBF */
99 GPRS_RLCMAC_ASSIGN, /* wait for downlink assignment */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200100 GPRS_RLCMAC_FLOW, /* RLC/MAC flow, ressource needed */
101 GPRS_RLCMAC_FINISHED, /* flow finished, wait for release */
Andreas Eversberge6228b32012-07-03 13:36:03 +0200102 GPRS_RLCMAC_WAIT_RELEASE,/* wait for release or restart of DL TBF */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200103 GPRS_RLCMAC_RELEASING, /* releasing, wait to free TBI/USF */
104};
105
106enum gprs_rlcmac_tbf_poll_state {
107 GPRS_RLCMAC_POLL_NONE = 0,
108 GPRS_RLCMAC_POLL_SCHED, /* a polling was scheduled */
109};
110
Andreas Eversberge6228b32012-07-03 13:36:03 +0200111enum gprs_rlcmac_tbf_dl_ass_state {
112 GPRS_RLCMAC_DL_ASS_NONE = 0,
113 GPRS_RLCMAC_DL_ASS_SEND_ASS, /* send downlink assignment on next RTS */
114 GPRS_RLCMAC_DL_ASS_WAIT_ACK, /* wait for PACKET CONTROL ACK */
115};
116
117enum gprs_rlcmac_tbf_ul_ass_state {
118 GPRS_RLCMAC_UL_ASS_NONE = 0,
119 GPRS_RLCMAC_UL_ASS_SEND_ASS, /* send uplink assignment on next RTS */
120 GPRS_RLCMAC_UL_ASS_WAIT_ACK, /* wait for PACKET CONTROL ACK */
121};
122
123enum gprs_rlcmac_tbf_ul_ack_state {
124 GPRS_RLCMAC_UL_ACK_NONE = 0,
125 GPRS_RLCMAC_UL_ACK_SEND_ACK, /* send acknowledge on next RTS */
126 GPRS_RLCMAC_UL_ACK_WAIT_ACK, /* wait for PACKET CONTROL ACK */
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400127};
128
129enum gprs_rlcmac_tbf_direction {
130 GPRS_RLCMAC_DL_TBF,
131 GPRS_RLCMAC_UL_TBF
132};
133
134struct gprs_rlcmac_tbf {
135 struct llist_head list;
136 enum gprs_rlcmac_tbf_state state;
137 enum gprs_rlcmac_tbf_direction direction;
138 uint8_t tfi;
139 uint32_t tlli;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200140 uint8_t tlli_valid;
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200141 uint8_t trx;
142 uint16_t arfcn;
143 uint8_t tsc;
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200144 uint8_t first_ts; /* first TS used by TBF */
145 uint8_t first_common_ts; /* first TS that the phone can send and
146 reveive simultaniously */
147 uint8_t control_ts; /* timeslot control messages and polling */
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200148 uint8_t ms_class;
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200149 struct gprs_rlcmac_pdch *pdch[8]; /* list of PDCHs allocated to TBF */
150 uint16_t ta;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200151 uint8_t llc_frame[LLC_MAX_LEN]; /* current DL or UL frame */
152 uint16_t llc_index; /* current write/read position of frame */
153 uint16_t llc_length; /* len of current DL LLC_frame, 0 == no frame */
Andreas Eversberg8b761a32012-07-20 21:50:31 +0200154 struct llist_head llc_queue; /* queued LLC DL data */
Andreas Eversberge6228b32012-07-03 13:36:03 +0200155
156 enum gprs_rlcmac_tbf_dl_ass_state dl_ass_state;
157 enum gprs_rlcmac_tbf_ul_ass_state ul_ass_state;
158 enum gprs_rlcmac_tbf_ul_ack_state ul_ack_state;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200159
160 enum gprs_rlcmac_tbf_poll_state poll_state;
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200161 uint32_t poll_fn; /* frame number to poll */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200162
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200163 uint16_t ws; /* window size */
164 uint16_t sns; /* sequence number space */
Andreas Eversberge6228b32012-07-03 13:36:03 +0200165
166 /* Please note that all variables here will be reset when changing
167 * from WAIT RELEASE back to FLOW state (re-use of TBF).
168 * All states that need reset must be in this struct, so this is why
169 * variables are in both (dl and ul) structs and not outside union.
170 */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200171 union {
172 struct {
Andreas Eversberge6228b32012-07-03 13:36:03 +0200173 uint16_t bsn; /* block sequence number */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200174 uint16_t v_s; /* send state */
175 uint16_t v_a; /* ack state */
176 char v_b[RLC_MAX_SNS/2]; /* acknowledge state array */
Andreas Eversberge6228b32012-07-03 13:36:03 +0200177 int32_t tx_counter; /* count all transmitted blocks */
178 uint8_t n3105; /* N3105 counter */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200179 } dl;
180 struct {
Andreas Eversberge6228b32012-07-03 13:36:03 +0200181 uint16_t bsn; /* block sequence number */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200182 uint16_t v_r; /* receive state */
183 uint16_t v_q; /* receive window state */
184 char v_n[RLC_MAX_SNS/2]; /* receive state array */
185 int32_t rx_counter; /* count all received blocks */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200186 uint8_t n3103; /* N3103 counter */
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200187 uint8_t usf[8]; /* list USFs per PDCH (timeslot) */
Andreas Eversberg08e93cd2012-07-26 08:13:06 +0200188 uint8_t contention_resolution_done; /* set after done */
189 uint8_t final_ack_sent; /* set if we sent final ack */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200190 } ul;
191 } dir;
192 uint8_t rlc_block[RLC_MAX_SNS/2][RLC_MAX_LEN]; /* block history */
193 uint8_t rlc_block_len[RLC_MAX_SNS/2]; /* block len of history */
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +0400194
195 struct osmo_timer_list timer;
196 unsigned int T; /* Txxxx number */
197 unsigned int num_T_exp; /* number of consecutive T expirations */
198
199 struct osmo_gsm_timer_list gsm_timer;
200 unsigned int fT; /* fTxxxx number */
201 unsigned int num_fT_exp; /* number of consecutive fT expirations */
Andreas Eversbergb3ded4c2012-07-24 10:47:24 +0200202
203 struct timeval bw_tv; /* timestamp for bandwidth calculation */
204 uint32_t bw_octets; /* number of octets transmitted since bw_tv */
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400205};
206
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200207extern struct llist_head gprs_rlcmac_ul_tbfs; /* list of uplink TBFs */
208extern struct llist_head gprs_rlcmac_dl_tbfs; /* list of downlink TBFs */
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400209
Andreas Eversberg2b914642012-07-19 13:06:26 +0200210/*
211 * paging entry
212 */
213struct gprs_rlcmac_paging {
214 struct llist_head list;
215 uint8_t chan_needed;
216 uint8_t identity_lv[9];
217};
218
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200219int tfi_alloc(enum gprs_rlcmac_tbf_direction dir, uint8_t *_trx, uint8_t *_ts,
220 uint8_t use_trx, uint8_t first_ts);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400221
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200222struct gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_tbf *old_tbf,
223 enum gprs_rlcmac_tbf_direction dir, uint8_t tfi, uint8_t trx,
224 uint8_t first_ts, uint8_t ms_class, uint8_t single_slot);
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200225
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200226struct gprs_rlcmac_tbf *tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts,
227 enum gprs_rlcmac_tbf_direction dir);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400228
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200229struct gprs_rlcmac_tbf *tbf_by_tlli(uint32_t tlli,
230 enum gprs_rlcmac_tbf_direction dir);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200231
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200232struct gprs_rlcmac_tbf *tbf_by_poll_fn(uint32_t fn, uint8_t trx, uint8_t ts);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400233
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200234void tbf_free(struct gprs_rlcmac_tbf *tbf);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400235
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200236int tbf_update(struct gprs_rlcmac_tbf *tbf);
237
238int tbf_assign_control_ts(struct gprs_rlcmac_tbf *tbf);
239
Andreas Eversberge6228b32012-07-03 13:36:03 +0200240void tbf_new_state(struct gprs_rlcmac_tbf *tbf,
241 enum gprs_rlcmac_tbf_state state);
242
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200243void tbf_timer_start(struct gprs_rlcmac_tbf *tbf, unsigned int T,
Andreas Eversberge6228b32012-07-03 13:36:03 +0200244 unsigned int seconds, unsigned int microseconds);
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200245
246void tbf_timer_stop(struct gprs_rlcmac_tbf *tbf);
247
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400248/* TS 44.060 Section 10.4.7 Table 10.4.7.1: Payload Type field */
249enum gprs_rlcmac_block_type {
250 GPRS_RLCMAC_DATA_BLOCK = 0x0,
251 GPRS_RLCMAC_CONTROL_BLOCK = 0x1,
252 GPRS_RLCMAC_CONTROL_BLOCK_OPT = 0x2,
253 GPRS_RLCMAC_RESERVED = 0x3
254};
255
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200256int gprs_rlcmac_rcv_block(uint8_t trx, uint8_t ts, uint8_t *data, uint8_t len,
257 uint32_t fn);
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400258
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200259int gprs_rlcmac_tx_ul_ud(gprs_rlcmac_tbf *tbf);
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400260
Andreas Eversberge6228b32012-07-03 13:36:03 +0200261void tbf_timer_cb(void *_tbf);
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400262
Andreas Eversberge6228b32012-07-03 13:36:03 +0200263int gprs_rlcmac_poll_timeout(struct gprs_rlcmac_tbf *tbf);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200264
265int gprs_rlcmac_rcv_rach(uint8_t ra, uint32_t Fn, int16_t qta);
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400266
Andreas Eversbergfc1dfd82012-07-26 10:50:23 +0200267int gprs_rlcmac_rcv_control_block(uint8_t trx, uint8_t ts, uint32_t fn,
268 uint8_t *data, uint8_t len);
269
270int gprs_rlcmac_rcv_downlink_ack(uint8_t trx, uint8_t ts, uint32_t fn, uint16_t tfi, uint8_t final, uint8_t ssn, uint8_t *rbb, uint8_t request);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400271
Andreas Eversberge6228b32012-07-03 13:36:03 +0200272struct msgb *gprs_rlcmac_send_packet_uplink_assignment(
273 struct gprs_rlcmac_tbf *tbf, uint32_t fn);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400274
Andreas Eversberge6228b32012-07-03 13:36:03 +0200275struct msgb *gprs_rlcmac_send_packet_downlink_assignment(
276 struct gprs_rlcmac_tbf *tbf, uint32_t fn);
Ivan Kluchnikovc320d862012-03-18 15:04:48 +0400277
Andreas Eversberg7f5352c2012-07-23 18:20:36 +0200278void gprs_rlcmac_trigger_downlink_assignment(struct gprs_rlcmac_tbf *tbf,
279 struct gprs_rlcmac_tbf *old_tbf, char *imsi);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400280
Andreas Eversberge6228b32012-07-03 13:36:03 +0200281int gprs_rlcmac_downlink_ack(struct gprs_rlcmac_tbf *tbf, uint8_t final,
282 uint8_t ssn, uint8_t *rbb);
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200283
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200284int gprs_rlcmac_rcv_data_block_acknowledged(uint8_t trx, uint8_t ts,
285 uint8_t *data, uint8_t len);
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200286
Andreas Eversberge6228b32012-07-03 13:36:03 +0200287struct msgb *gprs_rlcmac_send_data_block_acknowledged(
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200288 struct gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t ts);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200289
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200290struct msgb *gprs_rlcmac_send_uplink_ack(struct gprs_rlcmac_tbf *tbf,
291 uint32_t fn);
Ivan Kluchnikov9c795ab2012-05-24 23:12:59 +0400292
Andreas Eversberge6228b32012-07-03 13:36:03 +0200293int gprs_rlcmac_rcv_rts_block(uint8_t trx, uint8_t ts, uint16_t arfcn,
294 uint32_t fn, uint8_t block_nr);
295
Andreas Eversberg2b914642012-07-19 13:06:26 +0200296int gprs_rlcmac_add_paging(uint8_t chan_needed, uint8_t *identity_lv);
297
298struct gprs_rlcmac_paging *gprs_rlcmac_dequeue_paging(
299 struct gprs_rlcmac_pdch *pdch);
300
301struct msgb *gprs_rlcmac_send_packet_paging_request(
302 struct gprs_rlcmac_pdch *pdch);
Andreas Eversberga1503fa2012-07-22 08:58:09 +0200303
304extern "C" {
305#endif
306int alloc_algorithm_a(struct gprs_rlcmac_tbf *old_tbf,
307 struct gprs_rlcmac_tbf *tbf, uint32_t cust);
308
309int alloc_algorithm_b(struct gprs_rlcmac_tbf *old_tbf,
310 struct gprs_rlcmac_tbf *tbf, uint32_t cust);
311#ifdef __cplusplus
312}
Andreas Eversberg8b761a32012-07-20 21:50:31 +0200313#endif
Andreas Eversberg2b914642012-07-19 13:06:26 +0200314
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400315#endif // GPRS_RLCMAC_H