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 $
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"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/socket.h>
44 #include <sys/thread2.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"
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 *);
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
);
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
);
128 /*---------------------------------------------------------------------------*
129 * DL UNIT DATA REQUEST from Layer 3
130 *---------------------------------------------------------------------------*/
132 i4b_dl_unit_data_req(int unit
, struct mbuf
*m
)
135 NDBGL2(L2_PRIM
, "unit %d",unit
);
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
];
149 NDBGL2(L2_PRIM
, "unit %d",unit
);
151 switch(l2sc
->Q921_state
)
157 if(IF_QFULL(&l2sc
->i_queue
))
159 NDBGL2(L2_ERROR
, "i_queue full!!");
167 IF_ENQUEUE(&l2sc
->i_queue
, m
);
170 i4b_i_frame_queued_up(l2sc
);
175 NDBGL2(L2_ERROR
, "unit %d ERROR in state [%s], freeing mbuf", unit
, i4b_print_l2state(l2sc
));
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
;
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
;
209 /*---------------------------------------------------------------------------*
210 * i4b_l2_unit_init - place layer 2 unit into known state
211 *---------------------------------------------------------------------------*/
213 i4b_l2_unit_init(int unit
)
215 l2_softc_t
*l2sc
= &l2_softc
[unit
];
219 l2sc
->Q921_state
= ST_TEI_UNAS
;
220 l2sc
->tei_valid
= TEI_INVALID
;
228 l2sc
->l3initiated
= 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
;
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
];
264 NDBGL1(L1_PRIM
, "unit %d, status=%d, parm=%d", unit
, status
, parm
);
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
);
284 case STI_L1STAT
: /* state of layer 1 */
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
);
301 i4b_l2_unit_init(unit
);
302 NDBGL2(L2_ERROR
, "unit %d, cannot access S0 bus!", unit
);
306 NDBGL2(L2_ERROR
, "ERROR, unit %d, unknown status message!", unit
);
311 MDL_Status_Ind(unit
, status
, parm
); /* send up to layer 3 */
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
);
329 i4b_l2_unit_init(unit
);
333 MPH_Command_Req(unit
, command
, parm
);
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
];
346 NDBGL1(L1_PRIM
, "unit %d", unit
);
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!");
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!");
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!");
381 i4b_rxd_u_frame(unit
, m
);
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
);
393 #endif /* NI4BQ921 > 0 */