Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / net / i4b / layer1 / itjc / i4b_itjc_l1fsm.c
1 /*
2  * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b_itjc_l1fsm.c - NetJet-S layer 1 I.430 state machine
28  *      ------------------------------------------------------------
29  *
30  * $FreeBSD: src/sys/i4b/layer1/itjc/i4b_itjc_l1fsm.c,v 1.1.2.1 2001/08/10 14:08:39 obrien Exp $
31  * $DragonFly: src/sys/net/i4b/layer1/itjc/i4b_itjc_l1fsm.c,v 1.2 2003/06/17 04:28:40 dillon Exp $
32  *
33  *      last edit-date: [Wed Jan 10 17:16:33 2001]
34  *
35  *---------------------------------------------------------------------------*/
36
37 #include "itjc.h"
38 #include "pci.h"
39
40 #if (NITJC > 0) && (NPCI > 0)
41
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47
48 #include <machine/stdarg.h>
49 #include <machine/clock.h>
50
51 #include <net/if.h>
52
53 #include <machine/i4b_debug.h>
54 #include <machine/i4b_ioctl.h>
55 #include <machine/i4b_trace.h>
56
57 #include <i4b/layer1/isic/i4b_isic.h>
58 #include <i4b/layer1/isic/i4b_isac.h>
59 #include <i4b/layer1/isic/i4b_hscx.h>
60
61 #include <i4b/layer1/i4b_l1.h>
62
63 #include <i4b/include/i4b_global.h>
64
65 #include <i4b/include/i4b_mbuf.h>
66
67 #include <i4b/layer1/itjc/i4b_itjc_ext.h>
68
69 #if DO_I4B_DEBUG
70 static char *state_text[N_STATES] = {
71         "F3 Deactivated",
72         "F4 Awaiting Signal",
73         "F5 Identifying Input",
74         "F6 Synchronized",
75         "F7 Activated",
76         "F8 Lost Framing",
77         "Illegal State" 
78 };
79
80 static char *event_text[N_EVENTS] = {
81         "EV_PHAR PH_ACT_REQ",
82         "EV_T3 Timer 3 expired",
83         "EV_INFO0 INFO0 received",
84         "EV_RSY Level Detected",
85         "EV_INFO2 INFO2 received",
86         "EV_INFO48 INFO4 received",
87         "EV_INFO410 INFO4 received",
88         "EV_DR Deactivate Req",
89         "EV_PU Power UP",
90         "EV_DIS Disconnected",
91         "EV_EI Error Ind",
92         "Illegal Event"
93 };
94 #endif
95
96 /* Function prototypes */
97
98 static void timer3_expired (struct l1_softc *sc);
99 static void T3_start (struct l1_softc *sc);
100 static void T3_stop (struct l1_softc *sc);
101 static void F_T3ex (struct l1_softc *sc);
102 static void timer4_expired (struct l1_softc *sc);
103 static void T4_start (struct l1_softc *sc);
104 static void T4_stop (struct l1_softc *sc);
105 static void F_AI8 (struct l1_softc *sc);
106 static void F_AI10 (struct l1_softc *sc);
107 static void F_I01 (struct l1_softc *sc);
108 static void F_I02 (struct l1_softc *sc);
109 static void F_I03 (struct l1_softc *sc);
110 static void F_I2 (struct l1_softc *sc);
111 static void F_ill (struct l1_softc *sc);
112 static void F_NULL (struct l1_softc *sc);
113
114 /*---------------------------------------------------------------------------*
115  *      I.430 Timer T3 expire function
116  *---------------------------------------------------------------------------*/ 
117 static void
118 timer3_expired(struct l1_softc *sc)
119 {
120         if(sc->sc_I430T3)
121         {
122                 NDBGL1(L1_T_ERR, "state = %s", itjc_printstate(sc));
123                 sc->sc_I430T3 = 0;
124
125                 /* XXX try some recovery here XXX */
126
127                 itjc_recover(sc);
128
129                 sc->sc_init_tries++;    /* increment retry count */
130
131 /*XXX*/         if(sc->sc_init_tries > 4)
132                 {
133                         int s = SPLI4B();
134
135                         sc->sc_init_tries = 0;
136                         
137                         if(sc->sc_obuf2 != NULL)
138                         {
139                                 i4b_Dfreembuf(sc->sc_obuf2);
140                                 sc->sc_obuf2 = NULL;
141                         }
142                         if(sc->sc_obuf != NULL)
143                         {
144                                 i4b_Dfreembuf(sc->sc_obuf);
145                                 sc->sc_obuf = NULL;
146                                 sc->sc_freeflag = 0;
147                                 sc->sc_op = NULL;
148                                 sc->sc_ol = 0;
149                         }
150
151                         splx(s);
152
153                         i4b_l1_mph_status_ind(L0ITJCUNIT(sc->sc_unit), STI_NOL1ACC, 0, NULL);
154                 }
155                 
156                 itjc_next_state(sc, EV_T3);             
157         }
158         else
159         {
160                 NDBGL1(L1_T_ERR, "expired without starting it ....");
161         }
162 }
163
164 /*---------------------------------------------------------------------------*
165  *      I.430 Timer T3 start
166  *---------------------------------------------------------------------------*/ 
167 static void
168 T3_start(struct l1_softc *sc)
169 {
170         NDBGL1(L1_T_MSG, "state = %s", itjc_printstate(sc));
171         sc->sc_I430T3 = 1;
172         sc->sc_T3_callout = timeout((TIMEOUT_FUNC_T)timer3_expired,(struct l1_softc *)sc, 2*hz);
173 }
174
175 /*---------------------------------------------------------------------------*
176  *      I.430 Timer T3 stop
177  *---------------------------------------------------------------------------*/ 
178 static void
179 T3_stop(struct l1_softc *sc)
180 {
181         NDBGL1(L1_T_MSG, "state = %s", itjc_printstate(sc));
182
183         sc->sc_init_tries = 0;  /* init connect retry count */
184         
185         if(sc->sc_I430T3)
186         {
187                 sc->sc_I430T3 = 0;
188                 untimeout((TIMEOUT_FUNC_T)timer3_expired,(struct l1_softc *)sc, sc->sc_T3_callout);
189         }
190 }
191
192 /*---------------------------------------------------------------------------*
193  *      I.430 Timer T3 expiry
194  *---------------------------------------------------------------------------*/ 
195 static void
196 F_T3ex(struct l1_softc *sc)
197 {
198         NDBGL1(L1_F_MSG, "FSM function F_T3ex executing");
199         if(ctrl_desc[sc->sc_unit].protocol != PROTOCOL_D64S)
200                 i4b_l1_ph_deactivate_ind(L0ITJCUNIT(sc->sc_unit));
201 }
202
203 /*---------------------------------------------------------------------------*
204  *      Timer T4 expire function
205  *---------------------------------------------------------------------------*/ 
206 static void
207 timer4_expired(struct l1_softc *sc)
208 {
209         if(sc->sc_I430T4)
210         {
211                 NDBGL1(L1_T_MSG, "state = %s", itjc_printstate(sc));
212                 sc->sc_I430T4 = 0;
213                 i4b_l1_mph_status_ind(L0ITJCUNIT(sc->sc_unit), STI_PDEACT, 0, NULL);
214         }
215         else
216         {
217                 NDBGL1(L1_T_ERR, "expired without starting it ....");
218         }
219 }
220
221 /*---------------------------------------------------------------------------*
222  *      Timer T4 start
223  *---------------------------------------------------------------------------*/ 
224 static void
225 T4_start(struct l1_softc *sc)
226 {
227         NDBGL1(L1_T_MSG, "state = %s", itjc_printstate(sc));
228         sc->sc_I430T4 = 1;
229         sc->sc_T4_callout = timeout((TIMEOUT_FUNC_T)timer4_expired,(struct l1_softc *)sc, hz);
230 }
231
232 /*---------------------------------------------------------------------------*
233  *      Timer T4 stop
234  *---------------------------------------------------------------------------*/ 
235 static void
236 T4_stop(struct l1_softc *sc)
237 {
238         NDBGL1(L1_T_MSG, "state = %s", itjc_printstate(sc));
239
240         if(sc->sc_I430T4)
241         {
242                 sc->sc_I430T4 = 0;
243                 untimeout((TIMEOUT_FUNC_T)timer4_expired,(struct l1_softc *)sc, sc->sc_T4_callout);
244         }
245 }
246
247 /*---------------------------------------------------------------------------*
248  *      FSM function: received AI8
249  *---------------------------------------------------------------------------*/ 
250 static void
251 F_AI8(struct l1_softc *sc)
252 {
253         T4_stop(sc);
254
255         NDBGL1(L1_F_MSG, "FSM function F_AI8 executing");
256
257         if(ctrl_desc[sc->sc_unit].protocol != PROTOCOL_D64S)
258                 i4b_l1_ph_activate_ind(L0ITJCUNIT(sc->sc_unit));
259
260         T3_stop(sc);
261
262         if(sc->sc_trace & TRACE_I)
263         {
264                 i4b_trace_hdr_t hdr;
265                 char info = INFO4_8;
266                 
267                 hdr.unit = L0ITJCUNIT(sc->sc_unit);
268                 hdr.type = TRC_CH_I;
269                 hdr.dir = FROM_NT;
270                 hdr.count = 0;
271                 MICROTIME(hdr.time);
272                 i4b_l1_trace_ind(&hdr, 1, &info);
273         }
274 }
275
276 /*---------------------------------------------------------------------------*
277  *      FSM function: received AI10
278  *---------------------------------------------------------------------------*/ 
279 static void
280 F_AI10(struct l1_softc *sc)
281 {
282         T4_stop(sc);
283         
284         NDBGL1(L1_F_MSG, "FSM function F_AI10 executing");
285
286         if(ctrl_desc[sc->sc_unit].protocol != PROTOCOL_D64S)
287                 i4b_l1_ph_activate_ind(L0ITJCUNIT(sc->sc_unit));
288
289         T3_stop(sc);
290
291         if(sc->sc_trace & TRACE_I)
292         {
293                 i4b_trace_hdr_t hdr;
294                 char info = INFO4_10;
295                 
296                 hdr.unit = L0ITJCUNIT(sc->sc_unit);
297                 hdr.type = TRC_CH_I;
298                 hdr.dir = FROM_NT;
299                 hdr.count = 0;
300                 MICROTIME(hdr.time);
301                 i4b_l1_trace_ind(&hdr, 1, &info);
302         }
303 }
304
305 /*---------------------------------------------------------------------------*
306  *      FSM function: received INFO 0 in states F3 .. F5
307  *---------------------------------------------------------------------------*/ 
308 static void
309 F_I01(struct l1_softc *sc)
310 {
311         NDBGL1(L1_F_MSG, "FSM function F_I01 executing");
312
313         if(sc->sc_trace & TRACE_I)
314         {
315                 i4b_trace_hdr_t hdr;
316                 char info = INFO0;
317                 
318                 hdr.unit = L0ITJCUNIT(sc->sc_unit);
319                 hdr.type = TRC_CH_I;
320                 hdr.dir = FROM_NT;
321                 hdr.count = 0;
322                 MICROTIME(hdr.time);
323                 i4b_l1_trace_ind(&hdr, 1, &info);
324         }
325 }
326
327 /*---------------------------------------------------------------------------*
328  *      FSM function: received INFO 0 in state F6
329  *---------------------------------------------------------------------------*/ 
330 static void
331 F_I02(struct l1_softc *sc)
332 {
333         NDBGL1(L1_F_MSG, "FSM function F_I02 executing");
334
335         if(ctrl_desc[sc->sc_unit].protocol != PROTOCOL_D64S)
336                 i4b_l1_ph_deactivate_ind(L0ITJCUNIT(sc->sc_unit));
337
338         if(sc->sc_trace & TRACE_I)
339         {
340                 i4b_trace_hdr_t hdr;
341                 char info = INFO0;
342                 
343                 hdr.unit = L0ITJCUNIT(sc->sc_unit);
344                 hdr.type = TRC_CH_I;
345                 hdr.dir = FROM_NT;
346                 hdr.count = 0;
347                 MICROTIME(hdr.time);
348                 i4b_l1_trace_ind(&hdr, 1, &info);
349         }
350 }
351
352 /*---------------------------------------------------------------------------*
353  *      FSM function: received INFO 0 in state F7 or F8
354  *---------------------------------------------------------------------------*/ 
355 static void
356 F_I03(struct l1_softc *sc)
357 {
358         NDBGL1(L1_F_MSG, "FSM function F_I03 executing");
359
360         if(ctrl_desc[sc->sc_unit].protocol != PROTOCOL_D64S)
361                 i4b_l1_ph_deactivate_ind(L0ITJCUNIT(sc->sc_unit));
362
363         T4_start(sc);
364         
365         if(sc->sc_trace & TRACE_I)
366         {
367                 i4b_trace_hdr_t hdr;
368                 char info = INFO0;
369                 
370                 hdr.unit = L0ITJCUNIT(sc->sc_unit);
371                 hdr.type = TRC_CH_I;
372                 hdr.dir = FROM_NT;
373                 hdr.count = 0;
374                 MICROTIME(hdr.time);
375                 i4b_l1_trace_ind(&hdr, 1, &info);
376         }
377 }
378
379 /*---------------------------------------------------------------------------*
380  *      FSM function: activate request
381  *---------------------------------------------------------------------------*/ 
382 static void
383 F_AR(struct l1_softc *sc)
384 {
385         NDBGL1(L1_F_MSG, "FSM function F_AR executing");
386
387         if(sc->sc_trace & TRACE_I)
388         {
389                 i4b_trace_hdr_t hdr;
390                 char info = INFO1_8;
391                 
392                 hdr.unit = L0ITJCUNIT(sc->sc_unit);
393                 hdr.type = TRC_CH_I;
394                 hdr.dir = FROM_TE;
395                 hdr.count = 0;
396                 MICROTIME(hdr.time);
397                 i4b_l1_trace_ind(&hdr, 1, &info);
398         }
399
400         itjc_isac_l1_cmd(sc, CMD_AR8);
401
402         T3_start(sc);
403 }
404
405 /*---------------------------------------------------------------------------*
406  *      FSM function: received INFO2
407  *---------------------------------------------------------------------------*/ 
408 static void
409 F_I2(struct l1_softc *sc)
410 {
411         NDBGL1(L1_F_MSG, "FSM function F_I2 executing");
412
413         if(sc->sc_trace & TRACE_I)
414         {
415                 i4b_trace_hdr_t hdr;
416                 char info = INFO2;
417                 
418                 hdr.unit = L0ITJCUNIT(sc->sc_unit);
419                 hdr.type = TRC_CH_I;
420                 hdr.dir = FROM_NT;
421                 hdr.count = 0;
422                 MICROTIME(hdr.time);
423                 i4b_l1_trace_ind(&hdr, 1, &info);
424         }               
425
426 }
427
428 /*---------------------------------------------------------------------------*
429  *      illegal state default action
430  *---------------------------------------------------------------------------*/ 
431 static void
432 F_ill(struct l1_softc *sc)
433 {
434         NDBGL1(L1_F_ERR, "FSM function F_ill executing");
435 }
436
437 /*---------------------------------------------------------------------------*
438  *      No action
439  *---------------------------------------------------------------------------*/ 
440 static void
441 F_NULL(struct l1_softc *sc)
442 {
443         NDBGL1(L1_F_MSG, "FSM function F_NULL executing");
444 }
445
446
447 /*---------------------------------------------------------------------------*
448  *      layer 1 state transition table
449  *---------------------------------------------------------------------------*/ 
450 struct itjc_state_tab {
451         void (*func) (struct l1_softc *sc);     /* function to execute */
452         int newstate;                           /* next state */
453 } itjc_state_tab[N_EVENTS][N_STATES] = {
454
455 /* STATE:       F3                      F4                      F5                      F6                      F7                      F8                      ILLEGAL STATE     */
456 /* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
457 /* 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}},
458 /* 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}},
459 /* 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}},
460 /* 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}},
461 /* 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}},
462 /* 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}},
463 /* 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}},
464 /* 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}},
465 /* 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}},
466 /* 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}},
467 /* 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}},
468 /* 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}}
469 };
470
471 /*---------------------------------------------------------------------------*
472  *      event handler
473  *---------------------------------------------------------------------------*/ 
474 void
475 itjc_next_state(struct l1_softc *sc, int event)
476 {
477         int currstate, newstate;
478
479         if(event >= N_EVENTS)
480                 panic("i4b_l1fsm.c: event >= N_EVENTS\n");
481
482         currstate = sc->sc_I430state;
483
484         if(currstate >= N_STATES)
485                 panic("i4b_l1fsm.c: currstate >= N_STATES\n");  
486
487         newstate = itjc_state_tab[event][currstate].newstate;
488
489         if(newstate >= N_STATES)
490                 panic("i4b_l1fsm.c: newstate >= N_STATES\n");   
491         
492         NDBGL1(L1_F_MSG, "FSM event [%s]: [%s => %s]", event_text[event],
493                                            state_text[currstate],
494                                            state_text[newstate]);
495
496         (*itjc_state_tab[event][currstate].func)(sc);
497
498         if(newstate == ST_ILL)
499         {
500                 newstate = ST_F3;
501                 NDBGL1(L1_F_ERR, "FSM Illegal State ERROR, oldstate = %s, newstate = %s, event = %s!",
502                                         state_text[currstate],
503                                         state_text[newstate],
504                                         event_text[event]);
505         }
506
507         sc->sc_I430state = newstate;
508 }
509
510 #if DO_I4B_DEBUG
511 /*---------------------------------------------------------------------------*
512  *      return pointer to current state description
513  *---------------------------------------------------------------------------*/ 
514 char *
515 itjc_printstate(struct l1_softc *sc)
516 {
517         return((char *) state_text[sc->sc_I430state]);
518 }
519 #endif
520         
521 #endif /* NITJC > 0 */