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

#include <linux/delay.h>
#include <linux/timer.h>

#include "xhfc.h"
#include "xhfc_leb.h"

/* Software-implemented timers for the S/T state machine, in milliseconds */
#define T1_MS	100	/* NT activation timer.
			   100..1000ms according to Cologne/ETSI TBR4 (??)
			   100ms in Cologne's mISDN driver.  */
#define T3_MS	30000	/* TE activation timer.
			   according to I.430 "the value depends on the
			   subscriber loop transmission technique. The worst
			   case value is 30s." 
			   XXX in DAHDI's mess, this was 500ms on the first
			   time, and then the worst-case value of 30s, which I
			   guess makes sense. Try to see if any document says
			   to do that and maybe do the same here? Can't seem to
			   understand how mISDN handles the matter. */
#define F6_F7_MS 2	/* Wait before signalling the change to F7.
			   This is done because the XHFC might go to F7
			   for a short time during a F6->F3 transition when
			   receiving INFO0. The wait time should be
			   "about 1ms", this set to 2 to guarantee that the
			   wait will be long enough. */

enum te_state { F0, F1, F2, F3, F4, F5, F6, F7, F8 };
enum nt_state { G0, G1, G2, G3, G4 };

static void activation_timer_expiry(struct xhfc_span* s);
static void signal_f7_transition(struct xhfc_span* s);

static void handle_state_change_nt(struct xhfc_span* s);
static void handle_state_change_te(struct xhfc_span* s);

static void allow_G2_G3_transition(struct xhfc_span* s);

static void print_state(struct xhfc_span* s);

void start_state_machine(struct xhfc_span* s)
{
	u8 a_su_wr_sta = 0;

	SET_V_SU_LD_STA(a_su_wr_sta, 0);

	write_xhfc(s->xhfc, R_SU_SEL, s->port);
	write_xhfc(s->xhfc, A_SU_WR_STA, 0);
}

void activate_request(struct xhfc_span* s)
{
	u8 a_su_wr_sta = 0;
	int sta = GET_V_SU_STA(s->state);

	int impossible_te = !s->nt && 
		(sta == F2 || sta == F4 || sta == F5 || sta == F5);
	int impossible_nt = s->nt &&
		(sta == G2 || sta == G3);	
	int no_action_te = !s->nt &&
		(sta == F0 || sta == F6 || sta == F8);

	if (impossible_te || impossible_nt) {
		printk(KERN_INFO DRIVER_NAME ": "
				"activate impossible by definition of layer 1"
				" in state %c%d on port %d\n",
				(s->nt ? 'G' : 'F'), sta, portno(s));
		return;
	} else if (no_action_te) {
		if(DBG_ST && DBG_SPAN(s))
			printk(KERN_INFO DRIVER_NAME ": "
					"no action for activate "
					"in state %c%d on port %d\n",
					(s->nt ? 'G' : 'F'), sta, portno(s));
		return;
	}
	
	if (DBG_ST && DBG_SPAN(s))
		printk(KERN_DEBUG DRIVER_NAME
				": activate on port %d\n",
				portno(s));
	
	SET_V_SU_ACT(a_su_wr_sta, V_SU_ACT_ACTIVATE);

	write_xhfc(s->xhfc, R_SU_SEL, s->port);
	write_xhfc(s->xhfc, A_SU_WR_STA, a_su_wr_sta);

	if (!s->nt && sta == F3 && GET_V_SU_INFO0(s->state))
		s->activation_timer = s->xhfc->ticks + T3_MS; /* xref5 */
	else
		s->activation_timer = NOT_RUNNING;
		/* timer for NT activated by state change handler (xref3) */
}

void deactivate_request(struct xhfc_span* s)
{
	u8 a_su_wr_sta = 0;
	int sta = GET_V_SU_STA(s->state);

	if (s->nt && (sta == G1 || sta == G4)) {
		printk(KERN_INFO DRIVER_NAME ": "
				"deactivate impossible by definition of layer 1"
				" in state %c%d on port %d\n",
				(s->nt ? 'G' : 'F'), sta, portno(s));
		return;
	}
	
	if(DBG_ST && DBG_SPAN(s))
		printk(KERN_DEBUG DRIVER_NAME
				": deactivate on port %d\n",
				portno(s));

	SET_V_SU_ACT(a_su_wr_sta, V_SU_ACT_DEACTIVATE);

	write_xhfc(s->xhfc, R_SU_SEL, s->port);
	write_xhfc(s->xhfc, A_SU_WR_STA, a_su_wr_sta);

	if(s->nt && (sta == G2 || sta == G3))
		s->activation_timer = NOT_RUNNING;
}

/* This function is meant to be ran each time an interface changes state;
 * however, the state change is polled (during the timer interrupt; see
 * enable_interrupts and xhfc_interrupt). Therefore, it is possible that
 * two or more state changes happen on the same interface between two
 * interruptions and that this functions gets only called once. 
 * Additionally, even when using the ST state change interrupt, an interruption
 * can still be overlooked and lead to one of these conditions.
 *
 * However, there is no notable consequence because the actions are either:
 *
 * - Starting timers:
 *   - T3 is only started after an activation request, therefore we always now
 *     when T3 has to start (xref5)
 *   - T1 is only started when going to state G2, which can only yield G3
 *     automatically. This can be disallowed in V_G2_G3_EN so that the timer
 *     can be started before allowing the transition with V_SU_SET_G2_G3.
 *     (xref6)
 * - Stopping timers:
 *   - T1 is only stopped on G2->G3.
 *     From G3:
 *      - G3->G2: if the timer is already running, stop it (xref4)
 *      - on deactivate requests stop the timer
 *   - T3 is stopped on going to state F7. I don't think it's possible to
 *     guarantee this; however except F2->F7 which can not happen when T3 is
 *     running, every transition to F7 stops the timer, so there is no
 *     incoherent behaviour;
 * - Changing alarms in DAHDI, which will be done anyway. No point notifying
 *   something that lasts less that 1ms.
 */
void handle_state_change(struct xhfc_span* s)
{
	struct xhfc* x = s->xhfc;

	s->prev_state = s->state;

	write_xhfc(x, R_SU_SEL, s->port);
	s->state = read_xhfc(x, A_SU_RD_STA);

	if(DBG_ST && DBG_SPAN(s))
		print_state(s);

	if(unlikely(s->prev_state == s->state)) {
		printk(KERN_WARNING DRIVER_NAME ": a state change has been "
				"reported on port %d but it looks like the "
				"state has not changed?\n", portno(s));
		return;
	}

	if(s->nt)
		handle_state_change_nt(s);
	else
		handle_state_change_te(s);
}

static void handle_state_change_nt(struct xhfc_span* s)
{
	switch (GET_V_SU_STA(s->state)) {
		case G0:
		case G1:
		case G4:
			s->span.alarms = DAHDI_ALARM_RED;
			break;
		case G2:
			s->span.alarms = DAHDI_ALARM_YELLOW;
			if(s->activation_timer)
				s->activation_timer = NOT_RUNNING; /* xref4 */
			else {
				/* xref3, this should happen only if the
				 * previous state is not G3 */
				s->activation_timer = s->xhfc->ticks + T1_MS;
				allow_G2_G3_transition(s); /* xref6 */
			}

			break;
		case G3:
			s->span.alarms = DAHDI_ALARM_NONE;

			s->activation_timer = NOT_RUNNING;
			break;
	}
}

static void handle_state_change_te(struct xhfc_span* s)
{
	int sta = GET_V_SU_STA(s->state);

	if (sta != F7)
		s->f6_f7_transition = NOT_RUNNING;

	switch(sta) {
		case F0:
		case F1:
		case F2:
		case F3:
		case F4:
		case F8:
			s->span.alarms = DAHDI_ALARM_RED;
			break;
		case F5:
		case F6:
			s->span.alarms = DAHDI_ALARM_YELLOW;
			break;
		case F7:
			s->activation_timer = NOT_RUNNING;

			if (		GET_V_SU_STA(s->prev_state) == F6 && 
					GET_V_SU_INFO0(s->state)) {
				s->f6_f7_transition = s->xhfc->ticks + T1_MS;
				break;
			}

			signal_f7_transition(s);

			break;
	}
}

void handle_st_timers(struct xhfc_span* s)
{
	struct xhfc* x = s->xhfc;

	if(unlikely(s->activation_timer && s->activation_timer <= x->ticks))
		activation_timer_expiry(s);
	if(unlikely(s->f6_f7_transition && s->f6_f7_transition <= x->ticks))
		signal_f7_transition(s);
}

static void signal_f7_transition(struct xhfc_span* s)
{
	if(DBG_ST && DBG_SPAN(s))
		printk(KERN_DEBUG DRIVER_NAME ": signalling transition to F7 "
				"on port %d\n", portno(s));

	s->span.alarms = DAHDI_ALARM_NONE;
	s->f6_f7_transition = NOT_RUNNING;
}

static void activation_timer_expiry(struct xhfc_span* s)
{
	int sta = GET_V_SU_STA(s->state);

	int t1_expiry = s->nt && sta == G2;
	int t3_expiry = !s->nt && (sta == F4 || sta == F5 || sta == F8);

	if(DBG_ST && DBG_SPAN(s))
		printk(KERN_INFO DRIVER_NAME 
				": %s%s activation timeout"
				" in state %c%d on port %d\n",
				t1_expiry || t3_expiry ? "" : "no action for ",
				s->nt ? "NT" : "TE",
				s->nt ? 'G' : 'F',
				sta, portno(s));

	s->activation_timer = NOT_RUNNING;

	if (t1_expiry || t3_expiry)
		deactivate_request(s);
}

static void allow_G2_G3_transition(struct xhfc_span* s)
{
	u8 a_su_wr_sta = 0;

        SET_V_SU_SET_G2_G3(a_su_wr_sta, 1);

        write_xhfc(s->xhfc, R_SU_SEL, s->port);
        write_xhfc(s->xhfc, A_SU_WR_STA, a_su_wr_sta);

	/* The bit is automatically cleared on G2->G3. G2->G4 is always
	 * triggered by using activate_request which clears this bit. Therefore
	 * this bit is always cleared correctly when exiting G2. */
}

static char *state_description[2][16] = {
	{ /* TE */
		/* F0 */ "reset",
		/* F1 */ "inactive",
		/* F2 */ "sensing",
		/* F3 */ "deactivate",
		/* F4 */ "awaiting signal",
		/* F5 */ "identifying input",
		/* F6 */ "synchronized",
		/* F7 */ "activated",
		/* F8 */ "lost framing",
		"?", "?", "?", "?", "?", "?", "?" },
	{ /* NT */
		/* G0 */ "reset",
		/* G1 */ "deactivated",
		/* G2 */ "pending activation",
		/* G3 */ "active",
		/* G4 */ "pending deactivation",
		"?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?" }};

static void print_state(struct xhfc_span* s)
{
	int sta = GET_V_SU_STA(s->state);
	printk(KERN_DEBUG DRIVER_NAME ": port %d: %s state %c%d (%s)\n", 
			portno(s), 
			(s->nt ? "NT" : "TE"), 
			(s->nt ? 'G' : 'F'), 
			sta,
			state_description[s->nt][sta]);
}

#if 0
/* This is not used right now, but it could be in the future */
static void set_st_state(struct xhfc_span* s, int state)
{
	u8 a_su_wr_sta = 0;

	s->prev_state = s->state;
	s->state = state;

	if(DBG_ST && DBG_SPAN(s)) {
		printk(KERN_DEBUG DRIVER_NAME "port %d: "
				"Forcing state change:\n", s->port +1);
		print_state(s);
	}

	SET_V_SU_SET_STA(a_su_wr_sta, state);
	SET_V_SU_LD_STA(a_su_wr_sta, 1);

	write_xhfc(s->xhfc, R_SU_SEL, s->port);
	write_xhfc(s->xhfc, A_SU_WR_STA, a_su_wr_sta);

	udelay(6);

	SET_V_SU_LD_STA(a_su_wr_sta, 0);
	write_xhfc(s->xhfc, R_SU_SEL, s->port); /* just in case. (XXX?) */
	write_xhfc(s->xhfc, A_SU_WR_STA, a_su_wr_sta);
}
#endif