2 * Copyright (c) 1997, 1999 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 * $Id: i4b_l2.c,v 1.30 1999/12/13 21:25:27 hm Exp $
34 * last edit-date: [Mon Dec 13 22:03:23 1999]
36 *---------------------------------------------------------------------------*/
45 #include <sys/param.h>
47 #if defined(__FreeBSD__)
49 #include <sys/ioctl.h>
52 #include <sys/systm.h>
54 #include <sys/socket.h>
58 #include <machine/i4b_debug.h>
59 #include <machine/i4b_ioctl.h>
61 #include <i4b/i4b_debug.h>
62 #include <i4b/i4b_ioctl.h>
65 #include <i4b/include/i4b_l1l2.h>
66 #include <i4b/include/i4b_l2l3.h>
67 #include <i4b/include/i4b_isdnq931.h>
68 #include <i4b/include/i4b_mbuf.h>
69 #include <i4b/include/i4b_global.h>
71 #include <i4b/layer2/i4b_l2.h>
72 #include <i4b/layer2/i4b_l2fsm.h>
74 int i4b_dl_establish_ind(int);
75 int i4b_dl_establish_cnf(int);
76 int i4b_dl_release_ind(int);
77 int i4b_dl_release_cnf(int);
78 int i4b_dl_data_ind(int, struct mbuf *);
79 int i4b_dl_unit_data_ind(int, struct mbuf *);
81 static int i4b_mdl_command_req(int, int, int);
85 extern int i4b_mdl_attach_ind(int, int);
86 extern int i4b_mdl_status_ind(int, int, int);
88 /* this layers debug level */
90 unsigned int i4b_l2_debug = L2_DEBUG_DEFAULT;
92 struct i4b_l2l3_func i4b_l2l3_func = {
94 /* Layer 2 --> Layer 3 */
96 (int (*)(int)) i4b_dl_establish_ind,
97 (int (*)(int)) i4b_dl_establish_cnf,
98 (int (*)(int)) i4b_dl_release_ind,
99 (int (*)(int)) i4b_dl_release_cnf,
100 (int (*)(int, struct mbuf *)) i4b_dl_data_ind,
101 (int (*)(int, struct mbuf *)) i4b_dl_unit_data_ind,
103 /* Layer 3 --> Layer 2 */
105 (int (*)(int)) i4b_dl_establish_req,
106 (int (*)(int)) i4b_dl_release_req,
107 (int (*)(int, struct mbuf *)) i4b_dl_data_req,
108 (int (*)(int, struct mbuf *)) i4b_dl_unit_data_req,
110 /* Layer 2 --> Layer 3 management */
112 (int (*)(int, int, int)) i4b_mdl_status_ind,
114 /* Layer 3 --> Layer 2 management */
116 (int (*)(int, int, int)) i4b_mdl_command_req
119 /*---------------------------------------------------------------------------*
120 * DL_ESTABLISH_REQ from layer 3
121 *---------------------------------------------------------------------------*/
122 int i4b_dl_establish_req(int unit)
124 l2_softc_t *l2sc = &l2_softc[unit];
126 DBGL2(L2_PRIM, "DL-ESTABLISH-REQ", ("unit %d\n",unit));
127 i4b_l1_activate(l2sc);
128 i4b_next_l2state(l2sc, EV_DLESTRQ);
132 /*---------------------------------------------------------------------------*
133 * DL_RELEASE_REQ from layer 3
134 *---------------------------------------------------------------------------*/
135 int i4b_dl_release_req(int unit)
137 l2_softc_t *l2sc = &l2_softc[unit];
139 DBGL2(L2_PRIM, "DL-RELEASE-REQ", ("unit %d\n",unit));
140 i4b_next_l2state(l2sc, EV_DLRELRQ);
144 /*---------------------------------------------------------------------------*
145 * DL UNIT DATA REQUEST from Layer 3
146 *---------------------------------------------------------------------------*/
147 int i4b_dl_unit_data_req(int unit, struct mbuf *m)
150 DBGL2(L2_PRIM, "DL-UNIT-DATA-REQ", ("unit %d\n",unit));
155 /*---------------------------------------------------------------------------*
156 * DL DATA REQUEST from Layer 3
157 *---------------------------------------------------------------------------*/
158 int i4b_dl_data_req(int unit, struct mbuf *m)
160 l2_softc_t *l2sc = &l2_softc[unit];
163 DBGL2(L2_PRIM, "DL-DATA-REQ", ("unit %d\n",unit));
165 switch(l2sc->Q921_state)
171 if(IF_QFULL(&l2sc->i_queue))
173 DBGL2(L2_ERROR, "i4b_dl_data_req", ("i_queue full!!\n"));
181 IF_ENQUEUE(&l2sc->i_queue, m);
184 i4b_i_frame_queued_up(l2sc);
189 DBGL2(L2_ERROR, "i4b_dl_data_req", ("unit %d ERROR in state [%s], freeing mbuf\n", unit, i4b_print_l2state(l2sc)));
196 /*---------------------------------------------------------------------------*
197 * i4b_ph_activate_ind - link activation indication from layer 1
198 *---------------------------------------------------------------------------*/
200 i4b_ph_activate_ind(int unit)
202 l2_softc_t *l2sc = &l2_softc[unit];
204 DBGL1(L1_PRIM, "PH-ACTIVATE-IND", ("unit %d\n",unit));
205 l2sc->ph_active = PH_ACTIVE;
209 /*---------------------------------------------------------------------------*
210 * i4b_ph_deactivate_ind - link deactivation indication from layer 1
211 *---------------------------------------------------------------------------*/
213 i4b_ph_deactivate_ind(int unit)
215 l2_softc_t *l2sc = &l2_softc[unit];
217 DBGL1(L1_PRIM, "PH-DEACTIVATE-IND", ("unit %d\n",unit));
218 l2sc->ph_active = PH_INACTIVE;
223 /*---------------------------------------------------------------------------*
224 * i4b_l2_unit_init - place layer 2 unit into known state
225 *---------------------------------------------------------------------------*/
227 i4b_l2_unit_init(int unit)
229 l2_softc_t *l2sc = &l2_softc[unit];
233 l2sc->Q921_state = ST_TEI_UNAS;
234 l2sc->tei_valid = TEI_INVALID;
242 l2sc->l3initiated = 0;
248 l2sc->iframe_sent = 0;
250 l2sc->postfsmfunc = NULL;
252 if(l2sc->ua_num != UA_EMPTY)
254 i4b_Dfreembuf(l2sc->ua_frame);
255 l2sc->ua_num = UA_EMPTY;
256 l2sc->ua_frame = NULL;
266 /*---------------------------------------------------------------------------*
267 * i4b_mph_status_ind - status indication upward
268 *---------------------------------------------------------------------------*/
270 i4b_mph_status_ind(int unit, int status, int parm)
272 l2_softc_t *l2sc = &l2_softc[unit];
278 DBGL1(L1_PRIM, "MPH-STATUS-IND", ("unit %d, status=%d, parm=%d\n", unit, status, parm));
284 l2sc->i_queue.ifq_maxlen = IQUEUE_MAXLEN;
285 l2sc->ua_frame = NULL;
286 bzero(&l2sc->stat, sizeof(lapdstat_t));
287 i4b_l2_unit_init(unit);
289 #if defined(__FreeBSD__)
290 /* initialize the callout handles for timeout routines */
291 callout_handle_init(&l2sc->T200_callout);
292 callout_handle_init(&l2sc->T202_callout);
293 callout_handle_init(&l2sc->T203_callout);
294 callout_handle_init(&l2sc->IFQU_callout);
298 case STI_L1STAT: /* state of layer 1 */
301 case STI_PDEACT: /* Timer 4 expired */
302 /*XXX*/ if((l2sc->Q921_state >= ST_AW_EST) &&
303 (l2sc->Q921_state <= ST_TIMREC))
305 DBGL2(L2_ERROR, "i4b_mph_status_ind", ("unit %d, persistent deactivation!\n", unit));
306 i4b_l2_unit_init(unit);
315 i4b_l2_unit_init(unit);
316 DBGL2(L2_ERROR, "i4b_mph_status_ind", ("unit %d, cannot access S0 bus!\n", unit));
320 DBGL2(L2_ERROR, "i4b_mph_status_ind", ("ERROR, unit %d, unknown status message!\n", unit));
325 MDL_Status_Ind(unit, status, parm); /* send up to layer 3 */
332 /*---------------------------------------------------------------------------*
333 * MDL_COMMAND_REQ from layer 3
334 *---------------------------------------------------------------------------*/
335 int i4b_mdl_command_req(int unit, int command, int parm)
337 DBGL2(L2_PRIM, "MDL-COMMAND-REQ", ("unit %d, command=%d, parm=%d\n", unit, command, parm));
342 i4b_l2_unit_init(unit);
346 MPH_Command_Req(unit, command, parm);
351 /*---------------------------------------------------------------------------*
352 * i4b_ph_data_ind - process a rx'd frame got from layer 1
353 *---------------------------------------------------------------------------*/
355 i4b_ph_data_ind(int unit, struct mbuf *m)
357 l2_softc_t *l2sc = &l2_softc[unit];
359 DBGL1(L1_PRIM, "PH-DATA-IND", ("unit %d\n", unit));
361 u_char *ptr = m->m_data;
363 if ( (*(ptr + OFF_CNTL) & 0x01) == 0 )
365 if(m->m_len < 4) /* 6 oct - 2 chksum oct */
367 l2sc->stat.err_rx_len++;
368 DBGL2(L2_ERROR, "i4b_ph_data_ind", ("ERROR, I-frame < 6 octetts!\n"));
372 i4b_rxd_i_frame(unit, m);
374 else if ( (*(ptr + OFF_CNTL) & 0x03) == 0x01 )
376 if(m->m_len < 4) /* 6 oct - 2 chksum oct */
378 l2sc->stat.err_rx_len++;
379 DBGL2(L2_ERROR, "i4b_ph_data_ind", ("ERROR, S-frame < 6 octetts!\n"));
383 i4b_rxd_s_frame(unit, m);
385 else if ( (*(ptr + OFF_CNTL) & 0x03) == 0x03 )
387 if(m->m_len < 3) /* 5 oct - 2 chksum oct */
389 l2sc->stat.err_rx_len++;
390 DBGL2(L2_ERROR, "i4b_ph_data_ind", ("ERROR, U-frame < 5 octetts!\n"));
394 i4b_rxd_u_frame(unit, m);
398 l2sc->stat.err_rx_badf++;
399 DBGL2(L2_ERROR, "i4b_ph_data_ind", ("ERROR, bad frame rx'd - "));
400 i4b_print_frame(m->m_len, m->m_data);
406 #endif /* NI4BQ921 > 0 */