i4b(4): Initialize callout handles before first accessing them.
[dragonfly.git] / sys / net / i4b / layer2 / i4b_l2.c
blob6c26c282fdd08a1f90c3627adb56b4ed57e0a203
1 /*
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
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.
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.
25 *---------------------------------------------------------------------------
27 * i4b_l2.c - ISDN layer 2 (Q.921)
28 * -------------------------------
30 * $FreeBSD: src/sys/i4b/layer2/i4b_l2.c,v 1.6.2.1 2001/08/10 14:08:41 obrien Exp $
31 * $DragonFly: src/sys/net/i4b/layer2/i4b_l2.c,v 1.10 2006/01/14 11:05:18 swildner Exp $
33 * last edit-date: [Fri Jan 12 16:43:31 2001]
35 *---------------------------------------------------------------------------*/
37 #include "use_i4bq921.h"
38 #if NI4BQ921 > 0
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/thread2.h>
45 #include <net/if.h>
47 #include <net/i4b/include/machine/i4b_debug.h>
49 #include "../include/i4b_l1l2.h"
50 #include "../include/i4b_l2l3.h"
51 #include "../include/i4b_mbuf.h"
52 #include "../include/i4b_global.h"
54 #include "i4b_l2.h"
55 #include "i4b_l2fsm.h"
57 int i4b_dl_establish_ind(int);
58 int i4b_dl_establish_cnf(int);
59 int i4b_dl_release_ind(int);
60 int i4b_dl_release_cnf(int);
61 int i4b_dl_data_ind(int, struct mbuf *);
62 int i4b_dl_unit_data_ind(int, struct mbuf *);
64 static int i4b_mdl_command_req(int, int, void *);
66 /* from layer 2 */
68 extern int i4b_mdl_status_ind(int, int, int);
70 /* this layers debug level */
72 unsigned int i4b_l2_debug = L2_DEBUG_DEFAULT;
74 struct i4b_l2l3_func i4b_l2l3_func = {
76 /* Layer 2 --> Layer 3 */
78 (int (*)(int)) i4b_dl_establish_ind,
79 (int (*)(int)) i4b_dl_establish_cnf,
80 (int (*)(int)) i4b_dl_release_ind,
81 (int (*)(int)) i4b_dl_release_cnf,
82 (int (*)(int, struct mbuf *)) i4b_dl_data_ind,
83 (int (*)(int, struct mbuf *)) i4b_dl_unit_data_ind,
85 /* Layer 3 --> Layer 2 */
87 (int (*)(int)) i4b_dl_establish_req,
88 (int (*)(int)) i4b_dl_release_req,
89 (int (*)(int, struct mbuf *)) i4b_dl_data_req,
90 (int (*)(int, struct mbuf *)) i4b_dl_unit_data_req,
92 /* Layer 2 --> Layer 3 management */
94 (int (*)(int, int, int)) i4b_mdl_status_ind,
96 /* Layer 3 --> Layer 2 management */
98 (int (*)(int, int, void *)) i4b_mdl_command_req
101 /*---------------------------------------------------------------------------*
102 * DL_ESTABLISH_REQ from layer 3
103 *---------------------------------------------------------------------------*/
105 i4b_dl_establish_req(int unit)
107 l2_softc_t *l2sc = &l2_softc[unit];
109 NDBGL2(L2_PRIM, "unit %d",unit);
110 i4b_l1_activate(l2sc);
111 i4b_next_l2state(l2sc, EV_DLESTRQ);
112 return(0);
115 /*---------------------------------------------------------------------------*
116 * DL_RELEASE_REQ from layer 3
117 *---------------------------------------------------------------------------*/
119 i4b_dl_release_req(int unit)
121 l2_softc_t *l2sc = &l2_softc[unit];
123 NDBGL2(L2_PRIM, "unit %d",unit);
124 i4b_next_l2state(l2sc, EV_DLRELRQ);
125 return(0);
128 /*---------------------------------------------------------------------------*
129 * DL UNIT DATA REQUEST from Layer 3
130 *---------------------------------------------------------------------------*/
132 i4b_dl_unit_data_req(int unit, struct mbuf *m)
134 #ifdef NOTDEF
135 NDBGL2(L2_PRIM, "unit %d",unit);
136 #endif
137 return(0);
140 /*---------------------------------------------------------------------------*
141 * DL DATA REQUEST from Layer 3
142 *---------------------------------------------------------------------------*/
144 i4b_dl_data_req(int unit, struct mbuf *m)
146 l2_softc_t *l2sc = &l2_softc[unit];
148 #ifdef NOTDEF
149 NDBGL2(L2_PRIM, "unit %d",unit);
150 #endif
151 switch(l2sc->Q921_state)
153 case ST_AW_EST:
154 case ST_MULTIFR:
155 case ST_TIMREC:
157 if(IF_QFULL(&l2sc->i_queue))
159 NDBGL2(L2_ERROR, "i_queue full!!");
160 i4b_Dfreembuf(m);
162 else
164 CRIT_VAR;
166 CRIT_BEG;
167 IF_ENQUEUE(&l2sc->i_queue, m);
168 CRIT_END;
170 i4b_i_frame_queued_up(l2sc);
172 break;
174 default:
175 NDBGL2(L2_ERROR, "unit %d ERROR in state [%s], freeing mbuf", unit, i4b_print_l2state(l2sc));
176 i4b_Dfreembuf(m);
177 break;
179 return(0);
182 /*---------------------------------------------------------------------------*
183 * i4b_ph_activate_ind - link activation indication from layer 1
184 *---------------------------------------------------------------------------*/
186 i4b_ph_activate_ind(int unit)
188 l2_softc_t *l2sc = &l2_softc[unit];
190 NDBGL1(L1_PRIM, "unit %d",unit);
191 l2sc->ph_active = PH_ACTIVE;
192 return(0);
195 /*---------------------------------------------------------------------------*
196 * i4b_ph_deactivate_ind - link deactivation indication from layer 1
197 *---------------------------------------------------------------------------*/
199 i4b_ph_deactivate_ind(int unit)
201 l2_softc_t *l2sc = &l2_softc[unit];
203 NDBGL1(L1_PRIM, "unit %d",unit);
204 l2sc->ph_active = PH_INACTIVE;
205 return(0);
209 /*---------------------------------------------------------------------------*
210 * i4b_l2_unit_init - place layer 2 unit into known state
211 *---------------------------------------------------------------------------*/
212 static void
213 i4b_l2_unit_init(int unit)
215 l2_softc_t *l2sc = &l2_softc[unit];
216 CRIT_VAR;
218 CRIT_BEG;
219 l2sc->Q921_state = ST_TEI_UNAS;
220 l2sc->tei_valid = TEI_INVALID;
221 l2sc->vr = 0;
222 l2sc->vs = 0;
223 l2sc->va = 0;
224 l2sc->ack_pend = 0;
225 l2sc->rej_excpt = 0;
226 l2sc->peer_busy = 0;
227 l2sc->own_busy = 0;
228 l2sc->l3initiated = 0;
230 l2sc->rxd_CR = 0;
231 l2sc->rxd_PF = 0;
232 l2sc->rxd_NR = 0;
233 l2sc->RC = 0;
234 l2sc->iframe_sent = 0;
236 l2sc->postfsmfunc = NULL;
238 if(l2sc->ua_num != UA_EMPTY)
240 i4b_Dfreembuf(l2sc->ua_frame);
241 l2sc->ua_num = UA_EMPTY;
242 l2sc->ua_frame = NULL;
245 i4b_T200_stop(l2sc);
246 i4b_T202_stop(l2sc);
247 i4b_T203_stop(l2sc);
249 CRIT_END;
252 /*---------------------------------------------------------------------------*
253 * i4b_mph_status_ind - status indication upward
254 *---------------------------------------------------------------------------*/
256 i4b_mph_status_ind(int unit, int status, int parm)
258 l2_softc_t *l2sc = &l2_softc[unit];
259 int sendup = 1;
260 CRIT_VAR;
262 CRIT_BEG;
264 NDBGL1(L1_PRIM, "unit %d, status=%d, parm=%d", unit, status, parm);
266 switch(status)
268 case STI_ATTACH:
269 l2sc->unit = unit;
270 l2sc->i_queue.ifq_maxlen = IQUEUE_MAXLEN;
272 l2sc->ua_frame = NULL;
273 bzero(&l2sc->stat, sizeof(lapdstat_t));
275 /* initialize the callout handles for timeout routines */
276 callout_init(&l2sc->T200_timeout);
277 callout_init(&l2sc->T202_timeout);
278 callout_init(&l2sc->T203_timeout);
279 callout_init(&l2sc->IFQU_timeout);
281 i4b_l2_unit_init(unit);
282 break;
284 case STI_L1STAT: /* state of layer 1 */
285 break;
287 case STI_PDEACT: /* Timer 4 expired */
288 /*XXX*/ if((l2sc->Q921_state >= ST_AW_EST) &&
289 (l2sc->Q921_state <= ST_TIMREC))
291 NDBGL2(L2_ERROR, "unit %d, persistent deactivation!", unit);
292 i4b_l2_unit_init(unit);
294 else
296 sendup = 0;
298 break;
300 case STI_NOL1ACC:
301 i4b_l2_unit_init(unit);
302 NDBGL2(L2_ERROR, "unit %d, cannot access S0 bus!", unit);
303 break;
305 default:
306 NDBGL2(L2_ERROR, "ERROR, unit %d, unknown status message!", unit);
307 break;
310 if(sendup)
311 MDL_Status_Ind(unit, status, parm); /* send up to layer 3 */
313 CRIT_END;
315 return(0);
318 /*---------------------------------------------------------------------------*
319 * MDL_COMMAND_REQ from layer 3
320 *---------------------------------------------------------------------------*/
322 i4b_mdl_command_req(int unit, int command, void * parm)
324 NDBGL2(L2_PRIM, "unit %d, command=%d, parm=%d", unit, command, (unsigned int)parm);
326 switch(command)
328 case CMR_DOPEN:
329 i4b_l2_unit_init(unit);
330 break;
333 MPH_Command_Req(unit, command, parm);
335 return(0);
338 /*---------------------------------------------------------------------------*
339 * i4b_ph_data_ind - process a rx'd frame got from layer 1
340 *---------------------------------------------------------------------------*/
342 i4b_ph_data_ind(int unit, struct mbuf *m)
344 l2_softc_t *l2sc = &l2_softc[unit];
345 #ifdef NOTDEF
346 NDBGL1(L1_PRIM, "unit %d", unit);
347 #endif
348 u_char *ptr = m->m_data;
350 if ( (*(ptr + OFF_CNTL) & 0x01) == 0 )
352 if(m->m_len < 4) /* 6 oct - 2 chksum oct */
354 l2sc->stat.err_rx_len++;
355 NDBGL2(L2_ERROR, "ERROR, I-frame < 6 octetts!");
356 i4b_Dfreembuf(m);
357 return(0);
359 i4b_rxd_i_frame(unit, m);
361 else if ( (*(ptr + OFF_CNTL) & 0x03) == 0x01 )
363 if(m->m_len < 4) /* 6 oct - 2 chksum oct */
365 l2sc->stat.err_rx_len++;
366 NDBGL2(L2_ERROR, "ERROR, S-frame < 6 octetts!");
367 i4b_Dfreembuf(m);
368 return(0);
370 i4b_rxd_s_frame(unit, m);
372 else if ( (*(ptr + OFF_CNTL) & 0x03) == 0x03 )
374 if(m->m_len < 3) /* 5 oct - 2 chksum oct */
376 l2sc->stat.err_rx_len++;
377 NDBGL2(L2_ERROR, "ERROR, U-frame < 5 octetts!");
378 i4b_Dfreembuf(m);
379 return(0);
381 i4b_rxd_u_frame(unit, m);
383 else
385 l2sc->stat.err_rx_badf++;
386 NDBGL2(L2_ERROR, "ERROR, bad frame rx'd - ");
387 i4b_print_frame(m->m_len, m->m_data);
388 i4b_Dfreembuf(m);
390 return(0);
393 #endif /* NI4BQ921 > 0 */