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 $
34 * last edit-date: [Mon May 29 16:55:35 2000]
36 *---------------------------------------------------------------------------*/
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/socket.h>
51 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
52 #include <sys/callout.h>
56 #include <machine/i4b_debug.h>
58 #include <i4b/i4b_debug.h>
59 #include <i4b/i4b_ioctl.h>
62 #include <i4b/include/i4b_global.h>
63 #include <i4b/include/i4b_l1l2.h>
64 #include <i4b/include/i4b_mbuf.h>
66 #include <i4b/layer2/i4b_l2.h>
68 /*---------------------------------------------------------------------------*
69 * routine ESTABLISH DATA LINK (Q.921 03/93 page 83)
70 *---------------------------------------------------------------------------*/
72 i4b_establish_data_link(l2_softc_t *l2sc)
74 i4b_l1_activate(l2sc);
76 i4b_clear_exception_conditions(l2sc);
80 i4b_tx_sabme(l2sc, P1);
82 i4b_T200_restart(l2sc);
87 /*---------------------------------------------------------------------------*
88 * routine CLEAR EXCEPTION CONDITIONS (Q.921 03/93 page 83)
89 *---------------------------------------------------------------------------*/
91 i4b_clear_exception_conditions(l2_softc_t *l2sc)
97 /*XXX -------------------------------------------------------------- */
98 /*XXX is this really appropriate here or should it moved elsewhere ? */
100 i4b_Dcleanifq(&l2sc->i_queue);
102 if(l2sc->ua_num != UA_EMPTY)
104 i4b_Dfreembuf(l2sc->ua_frame);
105 l2sc->ua_num = UA_EMPTY;
107 /*XXX -------------------------------------------------------------- */
120 /*---------------------------------------------------------------------------*
121 * routine TRANSMIT ENQUIRE (Q.921 03/93 page 83)
122 *---------------------------------------------------------------------------*/
124 i4b_transmit_enquire(l2_softc_t *l2sc)
127 i4b_tx_rnr_command(l2sc, P1);
129 i4b_tx_rr_command(l2sc, P1);
133 i4b_T200_start(l2sc);
136 /*---------------------------------------------------------------------------*
137 * routine NR ERROR RECOVERY (Q.921 03/93 page 83)
138 *---------------------------------------------------------------------------*/
140 i4b_nr_error_recovery(l2_softc_t *l2sc)
142 i4b_mdl_error_ind(l2sc, "i4b_nr_error_recovery", MDL_ERR_J);
144 i4b_establish_data_link(l2sc);
146 l2sc->l3initiated = 0;
149 /*---------------------------------------------------------------------------*
150 * routine ENQUIRY RESPONSE (Q.921 03/93 page 84)
151 *---------------------------------------------------------------------------*/
153 i4b_enquiry_response(l2_softc_t *l2sc)
156 i4b_tx_rnr_response(l2sc, F1);
158 i4b_tx_rr_response(l2sc, F1);
163 /*---------------------------------------------------------------------------*
164 * routine INVOKE RETRANSMISSION (Q.921 03/93 page 84)
165 *---------------------------------------------------------------------------*/
167 i4b_invoke_retransmission(l2_softc_t *l2sc, int nr)
173 NDBGL2(L2_ERROR, "nr = %d", nr );
175 while(l2sc->vs != nr)
177 NDBGL2(L2_ERROR, "nr(%d) != vs(%d)", nr, l2sc->vs);
181 /* XXXXXXXXXXXXXXXXX */
183 if((l2sc->ua_num != UA_EMPTY) && (l2sc->vs == l2sc->ua_num))
185 if(_IF_QFULL(&l2sc->i_queue))
187 NDBGL2(L2_ERROR, "ERROR, I-queue full!");
191 IF_ENQUEUE(&l2sc->i_queue, l2sc->ua_frame);
192 l2sc->ua_num = UA_EMPTY;
197 NDBGL2(L2_ERROR, "ERROR, l2sc->vs = %d, l2sc->ua_num = %d ",l2sc->vs, l2sc->ua_num);
200 /* XXXXXXXXXXXXXXXXX */
202 i4b_i_frame_queued_up(l2sc);
208 /*---------------------------------------------------------------------------*
209 * routine ACKNOWLEDGE PENDING (Q.921 03/93 p 70)
210 *---------------------------------------------------------------------------*/
212 i4b_acknowledge_pending(l2_softc_t *l2sc)
217 i4b_tx_rr_response(l2sc, F0);
221 /*---------------------------------------------------------------------------*
222 * i4b_print_frame - just print the hex contents of a frame
223 *---------------------------------------------------------------------------*/
225 i4b_print_frame(int len, u_char *buf)
230 if (!(i4b_l2_debug & L2_ERROR)) /* XXXXXXXXXXXXXXXXXXXXX */
233 for(i = 0; i < len; i++)
234 printf(" 0x%x", buf[i]);
239 /*---------------------------------------------------------------------------*
240 * i4b_print_l2var - print some l2softc vars
241 *---------------------------------------------------------------------------*/
243 i4b_print_l2var(l2_softc_t *l2sc)
245 NDBGL2(L2_ERROR, "unit%d V(R)=%d, V(S)=%d, V(A)=%d,ACKP=%d,PBSY=%d,OBSY=%d",
255 /*---------------------------------------------------------------------------*
256 * got s or i frame, check if valid ack for last sent frame
257 *---------------------------------------------------------------------------*/
259 i4b_rxd_ack(l2_softc_t *l2sc, int nr)
263 NDBGL2(L2_ERROR, "N(R)=%d, UA=%d, V(R)=%d, V(S)=%d, V(A)=%d",
271 if(l2sc->ua_num != UA_EMPTY)
279 if(l2sc->ua_num != nr)
280 NDBGL2(L2_ERROR, "((N(R)-1)=%d) != (UA=%d) !!!", nr, l2sc->ua_num);
282 i4b_Dfreembuf(l2sc->ua_frame);
283 l2sc->ua_num = UA_EMPTY;
289 /*---------------------------------------------------------------------------*
290 * if not already active, activate layer 1
291 *---------------------------------------------------------------------------*/
293 i4b_l1_activate(l2_softc_t *l2sc)
295 if(l2sc->ph_active == PH_INACTIVE)
297 l2sc->ph_active = PH_ACTIVEPEND;
298 PH_Act_Req(l2sc->unit);
302 /*---------------------------------------------------------------------------*
303 * check for v(a) <= n(r) <= v(s)
304 * nr = receive sequence frame counter, va = acknowledge sequence frame
305 * counter and vs = transmit sequence frame counter
306 *---------------------------------------------------------------------------*/
308 i4b_l2_nr_ok(int nr, int va, int vs)
310 if((va > nr) && ((nr != 0) || (va != 127)))
312 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [1]", va, nr, vs);
316 if((nr > vs) && ((vs != 0) || (nr != 127)))
318 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [2]", va, nr, vs);
324 #endif /* NI4BQ921 > 0 */