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_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 $
32 * last edit-date: [Fri Jan 12 16:43:31 2001]
34 *---------------------------------------------------------------------------*/
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/socket.h>
49 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
50 #include <sys/callout.h>
54 #include <machine/i4b_debug.h>
56 #include <i4b/i4b_debug.h>
57 #include <i4b/i4b_ioctl.h>
60 #include <i4b/include/i4b_l1l2.h>
61 #include <i4b/include/i4b_l2l3.h>
62 #include <i4b/include/i4b_mbuf.h>
63 #include <i4b/include/i4b_global.h>
65 #include <i4b/layer2/i4b_l2.h>
66 #include <i4b/layer2/i4b_l2fsm.h>
68 int i4b_dl_establish_ind(int);
69 int i4b_dl_establish_cnf(int);
70 int i4b_dl_release_ind(int);
71 int i4b_dl_release_cnf(int);
72 int i4b_dl_data_ind(int, struct mbuf *);
73 int i4b_dl_unit_data_ind(int, struct mbuf *);
75 static int i4b_mdl_command_req(int, int, void *);
79 extern int i4b_mdl_status_ind(int, int, int);
81 /* this layers debug level */
83 unsigned int i4b_l2_debug = L2_DEBUG_DEFAULT;
85 struct i4b_l2l3_func i4b_l2l3_func = {
87 /* Layer 2 --> Layer 3 */
89 (int (*)(int)) i4b_dl_establish_ind,
90 (int (*)(int)) i4b_dl_establish_cnf,
91 (int (*)(int)) i4b_dl_release_ind,
92 (int (*)(int)) i4b_dl_release_cnf,
93 (int (*)(int, struct mbuf *)) i4b_dl_data_ind,
94 (int (*)(int, struct mbuf *)) i4b_dl_unit_data_ind,
96 /* Layer 3 --> Layer 2 */
98 (int (*)(int)) i4b_dl_establish_req,
99 (int (*)(int)) i4b_dl_release_req,
100 (int (*)(int, struct mbuf *)) i4b_dl_data_req,
101 (int (*)(int, struct mbuf *)) i4b_dl_unit_data_req,
103 /* Layer 2 --> Layer 3 management */
105 (int (*)(int, int, int)) i4b_mdl_status_ind,
107 /* Layer 3 --> Layer 2 management */
109 (int (*)(int, int, void *)) i4b_mdl_command_req
112 /*---------------------------------------------------------------------------*
113 * DL_ESTABLISH_REQ from layer 3
114 *---------------------------------------------------------------------------*/
115 int i4b_dl_establish_req(int unit)
117 l2_softc_t *l2sc = &l2_softc[unit];
119 NDBGL2(L2_PRIM, "unit %d",unit);
120 i4b_l1_activate(l2sc);
121 i4b_next_l2state(l2sc, EV_DLESTRQ);
125 /*---------------------------------------------------------------------------*
126 * DL_RELEASE_REQ from layer 3
127 *---------------------------------------------------------------------------*/
128 int i4b_dl_release_req(int unit)
130 l2_softc_t *l2sc = &l2_softc[unit];
132 NDBGL2(L2_PRIM, "unit %d",unit);
133 i4b_next_l2state(l2sc, EV_DLRELRQ);
137 /*---------------------------------------------------------------------------*
138 * DL UNIT DATA REQUEST from Layer 3
139 *---------------------------------------------------------------------------*/
140 int i4b_dl_unit_data_req(int unit, struct mbuf *m)
143 NDBGL2(L2_PRIM, "unit %d",unit);
148 /*---------------------------------------------------------------------------*
149 * DL DATA REQUEST from Layer 3
150 *---------------------------------------------------------------------------*/
151 int i4b_dl_data_req(int unit, struct mbuf *m)
153 l2_softc_t *l2sc = &l2_softc[unit];
156 NDBGL2(L2_PRIM, "unit %d",unit);
158 switch(l2sc->Q921_state)
164 if(_IF_QFULL(&l2sc->i_queue))
166 NDBGL2(L2_ERROR, "i_queue full!!");
174 IF_ENQUEUE(&l2sc->i_queue, m);
177 i4b_i_frame_queued_up(l2sc);
182 NDBGL2(L2_ERROR, "unit %d ERROR in state [%s], freeing mbuf", unit, i4b_print_l2state(l2sc));
189 /*---------------------------------------------------------------------------*
190 * i4b_ph_activate_ind - link activation indication from layer 1
191 *---------------------------------------------------------------------------*/
193 i4b_ph_activate_ind(int unit)
195 l2_softc_t *l2sc = &l2_softc[unit];
197 NDBGL1(L1_PRIM, "unit %d",unit);
198 l2sc->ph_active = PH_ACTIVE;
202 /*---------------------------------------------------------------------------*
203 * i4b_ph_deactivate_ind - link deactivation indication from layer 1
204 *---------------------------------------------------------------------------*/
206 i4b_ph_deactivate_ind(int unit)
208 l2_softc_t *l2sc = &l2_softc[unit];
210 NDBGL1(L1_PRIM, "unit %d",unit);
211 l2sc->ph_active = PH_INACTIVE;
216 /*---------------------------------------------------------------------------*
217 * i4b_l2_unit_init - place layer 2 unit into known state
218 *---------------------------------------------------------------------------*/
220 i4b_l2_unit_init(int unit)
222 l2_softc_t *l2sc = &l2_softc[unit];
226 l2sc->Q921_state = ST_TEI_UNAS;
227 l2sc->tei_valid = TEI_INVALID;
235 l2sc->l3initiated = 0;
241 l2sc->iframe_sent = 0;
243 l2sc->postfsmfunc = NULL;
245 if(l2sc->ua_num != UA_EMPTY)
247 i4b_Dfreembuf(l2sc->ua_frame);
248 l2sc->ua_num = UA_EMPTY;
249 l2sc->ua_frame = NULL;
259 /*---------------------------------------------------------------------------*
260 * i4b_mph_status_ind - status indication upward
261 *---------------------------------------------------------------------------*/
263 i4b_mph_status_ind(int unit, int status, int parm)
265 l2_softc_t *l2sc = &l2_softc[unit];
271 NDBGL1(L1_PRIM, "unit %d, status=%d, parm=%d", unit, status, parm);
277 l2sc->i_queue.ifq_maxlen = IQUEUE_MAXLEN;
279 #if defined(__FreeBSD__) && __FreeBSD__ > 4
280 mtx_init(&l2sc->i_queue.ifq_mtx, "i4b_l2sc", MTX_DEF);
282 l2sc->ua_frame = NULL;
283 bzero(&l2sc->stat, sizeof(lapdstat_t));
284 i4b_l2_unit_init(unit);
286 #if defined(__FreeBSD__)
287 /* initialize the callout handles for timeout routines */
288 callout_handle_init(&l2sc->T200_callout);
289 callout_handle_init(&l2sc->T202_callout);
290 callout_handle_init(&l2sc->T203_callout);
291 callout_handle_init(&l2sc->IFQU_callout);
293 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
294 /* initialize the callout handles for timeout routines */
295 callout_init(&l2sc->T200_callout);
296 callout_init(&l2sc->T202_callout);
297 callout_init(&l2sc->T203_callout);
298 callout_init(&l2sc->IFQU_callout);
303 case STI_L1STAT: /* state of layer 1 */
306 case STI_PDEACT: /* Timer 4 expired */
307 /*XXX*/ if((l2sc->Q921_state >= ST_AW_EST) &&
308 (l2sc->Q921_state <= ST_TIMREC))
310 NDBGL2(L2_ERROR, "unit %d, persistent deactivation!", unit);
311 i4b_l2_unit_init(unit);
320 i4b_l2_unit_init(unit);
321 NDBGL2(L2_ERROR, "unit %d, cannot access S0 bus!", unit);
325 NDBGL2(L2_ERROR, "ERROR, unit %d, unknown status message!", unit);
330 MDL_Status_Ind(unit, status, parm); /* send up to layer 3 */
337 /*---------------------------------------------------------------------------*
338 * MDL_COMMAND_REQ from layer 3
339 *---------------------------------------------------------------------------*/
340 int i4b_mdl_command_req(int unit, int command, void * parm)
342 NDBGL2(L2_PRIM, "unit %d, command=%d, parm=%d", unit, command, (unsigned int)parm);
347 i4b_l2_unit_init(unit);
351 MPH_Command_Req(unit, command, parm);
356 /*---------------------------------------------------------------------------*
357 * i4b_ph_data_ind - process a rx'd frame got from layer 1
358 *---------------------------------------------------------------------------*/
360 i4b_ph_data_ind(int unit, struct mbuf *m)
362 l2_softc_t *l2sc = &l2_softc[unit];
364 NDBGL1(L1_PRIM, "unit %d", unit);
366 u_char *ptr = m->m_data;
368 if ( (*(ptr + OFF_CNTL) & 0x01) == 0 )
370 if(m->m_len < 4) /* 6 oct - 2 chksum oct */
372 l2sc->stat.err_rx_len++;
373 NDBGL2(L2_ERROR, "ERROR, I-frame < 6 octetts!");
377 i4b_rxd_i_frame(unit, m);
379 else if ( (*(ptr + OFF_CNTL) & 0x03) == 0x01 )
381 if(m->m_len < 4) /* 6 oct - 2 chksum oct */
383 l2sc->stat.err_rx_len++;
384 NDBGL2(L2_ERROR, "ERROR, S-frame < 6 octetts!");
388 i4b_rxd_s_frame(unit, m);
390 else if ( (*(ptr + OFF_CNTL) & 0x03) == 0x03 )
392 if(m->m_len < 3) /* 5 oct - 2 chksum oct */
394 l2sc->stat.err_rx_len++;
395 NDBGL2(L2_ERROR, "ERROR, U-frame < 5 octetts!");
399 i4b_rxd_u_frame(unit, m);
403 l2sc->stat.err_rx_badf++;
404 NDBGL2(L2_ERROR, "ERROR, bad frame rx'd - ");
405 i4b_print_frame(m->m_len, m->m_data);
411 #endif /* NI4BQ921 > 0 */