2 * Copyright (c) 1997, 2000 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_util.c - layer 2 utility routines
28 * -------------------------------------
30 * $Id: i4b_util.c,v 1.26 2000/08/24 11:48:58 hm Exp $
32 * $FreeBSD: src/sys/i4b/layer2/i4b_util.c,v 1.6.2.1 2001/08/10 14:08:41 obrien Exp $
33 * $DragonFly: src/sys/net/i4b/layer2/i4b_util.c,v 1.7 2005/06/14 21:19:19 joerg Exp $
35 * last edit-date: [Mon May 29 16:55:35 2000]
37 *---------------------------------------------------------------------------*/
39 #include "use_i4bq921.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/socket.h>
46 #include <sys/thread2.h>
49 #include <net/i4b/include/machine/i4b_debug.h>
51 #include "../include/i4b_global.h"
52 #include "../include/i4b_l1l2.h"
53 #include "../include/i4b_mbuf.h"
57 /*---------------------------------------------------------------------------*
58 * routine ESTABLISH DATA LINK (Q.921 03/93 page 83)
59 *---------------------------------------------------------------------------*/
61 i4b_establish_data_link(l2_softc_t *l2sc)
63 i4b_l1_activate(l2sc);
65 i4b_clear_exception_conditions(l2sc);
69 i4b_tx_sabme(l2sc, P1);
71 i4b_T200_restart(l2sc);
76 /*---------------------------------------------------------------------------*
77 * routine CLEAR EXCEPTION CONDITIONS (Q.921 03/93 page 83)
78 *---------------------------------------------------------------------------*/
80 i4b_clear_exception_conditions(l2_softc_t *l2sc)
86 /*XXX -------------------------------------------------------------- */
87 /*XXX is this really appropriate here or should it moved elsewhere ? */
89 i4b_Dcleanifq(&l2sc->i_queue);
91 if(l2sc->ua_num != UA_EMPTY)
93 i4b_Dfreembuf(l2sc->ua_frame);
94 l2sc->ua_num = UA_EMPTY;
96 /*XXX -------------------------------------------------------------- */
109 /*---------------------------------------------------------------------------*
110 * routine TRANSMIT ENQUIRE (Q.921 03/93 page 83)
111 *---------------------------------------------------------------------------*/
113 i4b_transmit_enquire(l2_softc_t *l2sc)
116 i4b_tx_rnr_command(l2sc, P1);
118 i4b_tx_rr_command(l2sc, P1);
122 i4b_T200_start(l2sc);
125 /*---------------------------------------------------------------------------*
126 * routine NR ERROR RECOVERY (Q.921 03/93 page 83)
127 *---------------------------------------------------------------------------*/
129 i4b_nr_error_recovery(l2_softc_t *l2sc)
131 i4b_mdl_error_ind(l2sc, "i4b_nr_error_recovery", MDL_ERR_J);
133 i4b_establish_data_link(l2sc);
135 l2sc->l3initiated = 0;
138 /*---------------------------------------------------------------------------*
139 * routine ENQUIRY RESPONSE (Q.921 03/93 page 84)
140 *---------------------------------------------------------------------------*/
142 i4b_enquiry_response(l2_softc_t *l2sc)
145 i4b_tx_rnr_response(l2sc, F1);
147 i4b_tx_rr_response(l2sc, F1);
152 /*---------------------------------------------------------------------------*
153 * routine INVOKE RETRANSMISSION (Q.921 03/93 page 84)
154 *---------------------------------------------------------------------------*/
156 i4b_invoke_retransmission(l2_softc_t *l2sc, int nr)
162 NDBGL2(L2_ERROR, "nr = %d", nr );
164 while(l2sc->vs != nr)
166 NDBGL2(L2_ERROR, "nr(%d) != vs(%d)", nr, l2sc->vs);
170 /* XXXXXXXXXXXXXXXXX */
172 if((l2sc->ua_num != UA_EMPTY) && (l2sc->vs == l2sc->ua_num))
174 if(IF_QFULL(&l2sc->i_queue))
176 NDBGL2(L2_ERROR, "ERROR, I-queue full!");
180 IF_ENQUEUE(&l2sc->i_queue, l2sc->ua_frame);
181 l2sc->ua_num = UA_EMPTY;
186 NDBGL2(L2_ERROR, "ERROR, l2sc->vs = %d, l2sc->ua_num = %d ",l2sc->vs, l2sc->ua_num);
189 /* XXXXXXXXXXXXXXXXX */
191 i4b_i_frame_queued_up(l2sc);
197 /*---------------------------------------------------------------------------*
198 * routine ACKNOWLEDGE PENDING (Q.921 03/93 p 70)
199 *---------------------------------------------------------------------------*/
201 i4b_acknowledge_pending(l2_softc_t *l2sc)
206 i4b_tx_rr_response(l2sc, F0);
210 /*---------------------------------------------------------------------------*
211 * i4b_print_frame - just print the hex contents of a frame
212 *---------------------------------------------------------------------------*/
214 i4b_print_frame(int len, u_char *buf)
219 if (!(i4b_l2_debug & L2_ERROR)) /* XXXXXXXXXXXXXXXXXXXXX */
222 for(i = 0; i < len; i++)
223 printf(" 0x%x", buf[i]);
228 /*---------------------------------------------------------------------------*
229 * i4b_print_l2var - print some l2softc vars
230 *---------------------------------------------------------------------------*/
232 i4b_print_l2var(l2_softc_t *l2sc)
234 NDBGL2(L2_ERROR, "unit%d V(R)=%d, V(S)=%d, V(A)=%d,ACKP=%d,PBSY=%d,OBSY=%d",
244 /*---------------------------------------------------------------------------*
245 * got s or i frame, check if valid ack for last sent frame
246 *---------------------------------------------------------------------------*/
248 i4b_rxd_ack(l2_softc_t *l2sc, int nr)
252 NDBGL2(L2_ERROR, "N(R)=%d, UA=%d, V(R)=%d, V(S)=%d, V(A)=%d",
260 if(l2sc->ua_num != UA_EMPTY)
268 if(l2sc->ua_num != nr)
269 NDBGL2(L2_ERROR, "((N(R)-1)=%d) != (UA=%d) !!!", nr, l2sc->ua_num);
271 i4b_Dfreembuf(l2sc->ua_frame);
272 l2sc->ua_num = UA_EMPTY;
278 /*---------------------------------------------------------------------------*
279 * if not already active, activate layer 1
280 *---------------------------------------------------------------------------*/
282 i4b_l1_activate(l2_softc_t *l2sc)
284 if(l2sc->ph_active == PH_INACTIVE)
286 l2sc->ph_active = PH_ACTIVEPEND;
287 PH_Act_Req(l2sc->unit);
291 /*---------------------------------------------------------------------------*
292 * check for v(a) <= n(r) <= v(s)
293 * nr = receive sequence frame counter, va = acknowledge sequence frame
294 * counter and vs = transmit sequence frame counter
295 *---------------------------------------------------------------------------*/
297 i4b_l2_nr_ok(int nr, int va, int vs)
299 if((va > nr) && ((nr != 0) || (va != 127)))
301 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [1]", va, nr, vs);
305 if((nr > vs) && ((vs != 0) || (nr != 127)))
307 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [2]", va, nr, vs);
313 #endif /* NI4BQ921 > 0 */