2 * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 *---------------------------------------------------------------------------
27 * i4b_l1fsm.c - isdn4bsd layer 1 I.430 state machine
28 * --------------------------------------------------
30 * $FreeBSD: src/sys/i4b/layer1/isic/i4b_l1fsm.c,v 1.5.2.1 2001/08/10 14:08:38 obrien Exp $
31 * $DragonFly: src/sys/net/i4b/layer1/isic/i4b_l1fsm.c,v 1.4 2004/09/16 04:36:32 dillon Exp $
33 * last edit-date: [Wed Jan 24 09:12:18 2001]
35 *---------------------------------------------------------------------------*/
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/systm.h>
44 #include <sys/socket.h>
48 #include <net/i4b/include/machine/i4b_debug.h>
49 #include <net/i4b/include/machine/i4b_ioctl.h>
50 #include <net/i4b/include/machine/i4b_trace.h>
54 #include "../i4b_l1.h"
55 #include "../../include/i4b_global.h"
56 #include "../../include/i4b_mbuf.h"
59 static char *state_text[N_STATES] = {
62 "F5 Identifying Input",
69 static char *event_text[N_EVENTS] = {
71 "EV_T3 Timer 3 expired",
72 "EV_INFO0 INFO0 received",
73 "EV_RSY Level Detected",
74 "EV_INFO2 INFO2 received",
75 "EV_INFO48 INFO4 received",
76 "EV_INFO410 INFO4 received",
77 "EV_DR Deactivate Req",
79 "EV_DIS Disconnected",
85 /* Function prototypes */
87 static void timer3_expired (struct l1_softc *sc);
88 static void T3_start (struct l1_softc *sc);
89 static void T3_stop (struct l1_softc *sc);
90 static void F_T3ex (struct l1_softc *sc);
91 static void timer4_expired (struct l1_softc *sc);
92 static void T4_start (struct l1_softc *sc);
93 static void T4_stop (struct l1_softc *sc);
94 static void F_AI8 (struct l1_softc *sc);
95 static void F_AI10 (struct l1_softc *sc);
96 static void F_I01 (struct l1_softc *sc);
97 static void F_I02 (struct l1_softc *sc);
98 static void F_I03 (struct l1_softc *sc);
99 static void F_I2 (struct l1_softc *sc);
100 static void F_ill (struct l1_softc *sc);
101 static void F_NULL (struct l1_softc *sc);
103 /*---------------------------------------------------------------------------*
104 * I.430 Timer T3 expire function
105 *---------------------------------------------------------------------------*/
107 timer3_expired(struct l1_softc *sc)
111 NDBGL1(L1_T_ERR, "state = %s", isic_printstate(sc));
114 /* XXX try some recovery here XXX */
118 sc->sc_init_tries++; /* increment retry count */
120 /*XXX*/ if(sc->sc_init_tries > 4)
124 sc->sc_init_tries = 0;
126 if(sc->sc_obuf2 != NULL)
128 i4b_Dfreembuf(sc->sc_obuf2);
131 if(sc->sc_obuf != NULL)
133 i4b_Dfreembuf(sc->sc_obuf);
142 i4b_l1_mph_status_ind(L0ISICUNIT(sc->sc_unit), STI_NOL1ACC, 0, NULL);
145 isic_next_state(sc, EV_T3);
149 NDBGL1(L1_T_ERR, "expired without starting it ....");
153 /*---------------------------------------------------------------------------*
154 * I.430 Timer T3 start
155 *---------------------------------------------------------------------------*/
157 T3_start(struct l1_softc *sc)
159 NDBGL1(L1_T_MSG, "state = %s", isic_printstate(sc));
161 callout_reset(&sc->sc_T3_timeout, 2 * hz,
162 (TIMEOUT_FUNC_T)timer3_expired, (struct l1_softc *)sc);
165 /*---------------------------------------------------------------------------*
166 * I.430 Timer T3 stop
167 *---------------------------------------------------------------------------*/
169 T3_stop(struct l1_softc *sc)
171 NDBGL1(L1_T_MSG, "state = %s", isic_printstate(sc));
173 sc->sc_init_tries = 0; /* init connect retry count */
178 callout_stop(&sc->sc_T3_timeout);
182 /*---------------------------------------------------------------------------*
183 * I.430 Timer T3 expiry
184 *---------------------------------------------------------------------------*/
186 F_T3ex(struct l1_softc *sc)
188 NDBGL1(L1_F_MSG, "FSM function F_T3ex executing");
189 if(ctrl_desc[sc->sc_unit].protocol != PROTOCOL_D64S)
190 i4b_l1_ph_deactivate_ind(L0ISICUNIT(sc->sc_unit));
193 /*---------------------------------------------------------------------------*
194 * Timer T4 expire function
195 *---------------------------------------------------------------------------*/
197 timer4_expired(struct l1_softc *sc)
201 NDBGL1(L1_T_MSG, "state = %s", isic_printstate(sc));
203 i4b_l1_mph_status_ind(L0ISICUNIT(sc->sc_unit), STI_PDEACT, 0, NULL);
207 NDBGL1(L1_T_ERR, "expired without starting it ....");
211 /*---------------------------------------------------------------------------*
213 *---------------------------------------------------------------------------*/
215 T4_start(struct l1_softc *sc)
217 NDBGL1(L1_T_MSG, "state = %s", isic_printstate(sc));
219 callout_reset(&sc->sc_T4_timeout, hz,
220 (TIMEOUT_FUNC_T)timer4_expired, (struct l1_softc *)sc);
223 /*---------------------------------------------------------------------------*
225 *---------------------------------------------------------------------------*/
227 T4_stop(struct l1_softc *sc)
229 NDBGL1(L1_T_MSG, "state = %s", isic_printstate(sc));
234 callout_stop(&sc->sc_T4_timeout);
238 /*---------------------------------------------------------------------------*
239 * FSM function: received AI8
240 *---------------------------------------------------------------------------*/
242 F_AI8(struct l1_softc *sc)
246 NDBGL1(L1_F_MSG, "FSM function F_AI8 executing");
248 if(ctrl_desc[sc->sc_unit].protocol != PROTOCOL_D64S)
249 i4b_l1_ph_activate_ind(L0ISICUNIT(sc->sc_unit));
253 if(sc->sc_trace & TRACE_I)
258 hdr.unit = L0ISICUNIT(sc->sc_unit);
263 i4b_l1_trace_ind(&hdr, 1, &info);
267 /*---------------------------------------------------------------------------*
268 * FSM function: received AI10
269 *---------------------------------------------------------------------------*/
271 F_AI10(struct l1_softc *sc)
275 NDBGL1(L1_F_MSG, "FSM function F_AI10 executing");
277 if(ctrl_desc[sc->sc_unit].protocol != PROTOCOL_D64S)
278 i4b_l1_ph_activate_ind(L0ISICUNIT(sc->sc_unit));
282 if(sc->sc_trace & TRACE_I)
285 char info = INFO4_10;
287 hdr.unit = L0ISICUNIT(sc->sc_unit);
292 i4b_l1_trace_ind(&hdr, 1, &info);
296 /*---------------------------------------------------------------------------*
297 * FSM function: received INFO 0 in states F3 .. F5
298 *---------------------------------------------------------------------------*/
300 F_I01(struct l1_softc *sc)
302 NDBGL1(L1_F_MSG, "FSM function F_I01 executing");
304 if(sc->sc_trace & TRACE_I)
309 hdr.unit = L0ISICUNIT(sc->sc_unit);
314 i4b_l1_trace_ind(&hdr, 1, &info);
318 /*---------------------------------------------------------------------------*
319 * FSM function: received INFO 0 in state F6
320 *---------------------------------------------------------------------------*/
322 F_I02(struct l1_softc *sc)
324 NDBGL1(L1_F_MSG, "FSM function F_I02 executing");
326 if(ctrl_desc[sc->sc_unit].protocol != PROTOCOL_D64S)
327 i4b_l1_ph_deactivate_ind(L0ISICUNIT(sc->sc_unit));
329 if(sc->sc_trace & TRACE_I)
334 hdr.unit = L0ISICUNIT(sc->sc_unit);
339 i4b_l1_trace_ind(&hdr, 1, &info);
343 /*---------------------------------------------------------------------------*
344 * FSM function: received INFO 0 in state F7 or F8
345 *---------------------------------------------------------------------------*/
347 F_I03(struct l1_softc *sc)
349 NDBGL1(L1_F_MSG, "FSM function F_I03 executing");
351 if(ctrl_desc[sc->sc_unit].protocol != PROTOCOL_D64S)
352 i4b_l1_ph_deactivate_ind(L0ISICUNIT(sc->sc_unit));
356 if(sc->sc_trace & TRACE_I)
361 hdr.unit = L0ISICUNIT(sc->sc_unit);
366 i4b_l1_trace_ind(&hdr, 1, &info);
370 /*---------------------------------------------------------------------------*
371 * FSM function: activate request
372 *---------------------------------------------------------------------------*/
374 F_AR(struct l1_softc *sc)
376 NDBGL1(L1_F_MSG, "FSM function F_AR executing");
378 if(sc->sc_trace & TRACE_I)
383 hdr.unit = L0ISICUNIT(sc->sc_unit);
388 i4b_l1_trace_ind(&hdr, 1, &info);
391 isic_isac_l1_cmd(sc, CMD_AR8);
396 /*---------------------------------------------------------------------------*
397 * FSM function: received INFO2
398 *---------------------------------------------------------------------------*/
400 F_I2(struct l1_softc *sc)
402 NDBGL1(L1_F_MSG, "FSM function F_I2 executing");
404 if(sc->sc_trace & TRACE_I)
409 hdr.unit = L0ISICUNIT(sc->sc_unit);
414 i4b_l1_trace_ind(&hdr, 1, &info);
419 /*---------------------------------------------------------------------------*
420 * illegal state default action
421 *---------------------------------------------------------------------------*/
423 F_ill(struct l1_softc *sc)
425 NDBGL1(L1_F_ERR, "FSM function F_ill executing");
428 /*---------------------------------------------------------------------------*
430 *---------------------------------------------------------------------------*/
432 F_NULL(struct l1_softc *sc)
434 NDBGL1(L1_F_MSG, "FSM function F_NULL executing");
438 /*---------------------------------------------------------------------------*
439 * layer 1 state transition table
440 *---------------------------------------------------------------------------*/
441 struct isic_state_tab {
442 void (*func) (struct l1_softc *sc); /* function to execute */
443 int newstate; /* next state */
444 } isic_state_tab[N_EVENTS][N_STATES] = {
446 /* STATE: F3 F4 F5 F6 F7 F8 ILLEGAL STATE */
447 /* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
448 /* EV_PHAR x*/ {{F_AR, ST_F4}, {F_NULL, ST_F4}, {F_NULL, ST_F5}, {F_NULL, ST_F6}, {F_ill, ST_ILL}, {F_NULL, ST_F8}, {F_ill, ST_ILL}},
449 /* EV_T3 x*/ {{F_NULL, ST_F3}, {F_T3ex, ST_F3}, {F_T3ex, ST_F3}, {F_T3ex, ST_F3}, {F_NULL, ST_F7}, {F_NULL, ST_F8}, {F_ill, ST_ILL}},
450 /* EV_INFO0 */ {{F_I01, ST_F3}, {F_I01, ST_F4}, {F_I01, ST_F5}, {F_I02, ST_F3}, {F_I03, ST_F3}, {F_I03, ST_F3}, {F_ill, ST_ILL}},
451 /* EV_RSY x*/ {{F_NULL, ST_F3}, {F_NULL, ST_F5}, {F_NULL, ST_F5}, {F_NULL, ST_F8}, {F_NULL, ST_F8}, {F_NULL, ST_F8}, {F_ill, ST_ILL}},
452 /* EV_INFO2 */ {{F_I2, ST_F6}, {F_I2, ST_F6}, {F_I2, ST_F6}, {F_I2, ST_F6}, {F_I2, ST_F6}, {F_I2, ST_F6}, {F_ill, ST_ILL}},
453 /* EV_INFO48*/ {{F_AI8, ST_F7}, {F_AI8, ST_F7}, {F_AI8, ST_F7}, {F_AI8, ST_F7}, {F_NULL, ST_F7}, {F_AI8, ST_F7}, {F_ill, ST_ILL}},
454 /* EV_INFO41*/ {{F_AI10, ST_F7}, {F_AI10, ST_F7}, {F_AI10, ST_F7}, {F_AI10, ST_F7}, {F_NULL, ST_F7}, {F_AI10, ST_F7}, {F_ill, ST_ILL}},
455 /* EV_DR */ {{F_NULL, ST_F3}, {F_NULL, ST_F4}, {F_NULL, ST_F5}, {F_NULL, ST_F6}, {F_NULL, ST_F7}, {F_NULL, ST_F8}, {F_ill, ST_ILL}},
456 /* EV_PU */ {{F_NULL, ST_F3}, {F_NULL, ST_F4}, {F_NULL, ST_F5}, {F_NULL, ST_F6}, {F_NULL, ST_F7}, {F_NULL, ST_F8}, {F_ill, ST_ILL}},
457 /* EV_DIS */ {{F_ill, ST_ILL}, {F_ill, ST_ILL}, {F_ill, ST_ILL}, {F_ill, ST_ILL}, {F_ill, ST_ILL}, {F_ill, ST_ILL}, {F_ill, ST_ILL}},
458 /* EV_EI */ {{F_NULL, ST_F3}, {F_NULL, ST_F3}, {F_NULL, ST_F3}, {F_NULL, ST_F3}, {F_NULL, ST_F3}, {F_NULL, ST_F3}, {F_ill, ST_ILL}},
459 /* EV_ILL */ {{F_ill, ST_ILL}, {F_ill, ST_ILL}, {F_ill, ST_ILL}, {F_ill, ST_ILL}, {F_ill, ST_ILL}, {F_ill, ST_ILL}, {F_ill, ST_ILL}}
462 /*---------------------------------------------------------------------------*
464 *---------------------------------------------------------------------------*/
466 isic_next_state(struct l1_softc *sc, int event)
468 int currstate, newstate;
470 if(event >= N_EVENTS)
471 panic("i4b_l1fsm.c: event >= N_EVENTS\n");
473 currstate = sc->sc_I430state;
475 if(currstate >= N_STATES)
476 panic("i4b_l1fsm.c: currstate >= N_STATES\n");
478 newstate = isic_state_tab[event][currstate].newstate;
480 if(newstate >= N_STATES)
481 panic("i4b_l1fsm.c: newstate >= N_STATES\n");
483 NDBGL1(L1_F_MSG, "FSM event [%s]: [%s => %s]", event_text[event],
484 state_text[currstate],
485 state_text[newstate]);
487 (*isic_state_tab[event][currstate].func)(sc);
489 if(newstate == ST_ILL)
492 NDBGL1(L1_F_ERR, "FSM Illegal State ERROR, oldstate = %s, newstate = %s, event = %s!",
493 state_text[currstate],
494 state_text[newstate],
498 sc->sc_I430state = newstate;
502 /*---------------------------------------------------------------------------*
503 * return pointer to current state description
504 *---------------------------------------------------------------------------*/
506 isic_printstate(struct l1_softc *sc)
508 return((char *) state_text[sc->sc_I430state]);
512 #endif /* NISIC > 0 */