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.2 2003/06/17 04:28:40 dillon Exp $
35 * last edit-date: [Mon May 29 16:55:35 2000]
37 *---------------------------------------------------------------------------*/
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/socket.h>
52 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
53 #include <sys/callout.h>
57 #include <machine/i4b_debug.h>
59 #include <i4b/i4b_debug.h>
60 #include <i4b/i4b_ioctl.h>
63 #include <i4b/include/i4b_global.h>
64 #include <i4b/include/i4b_l1l2.h>
65 #include <i4b/include/i4b_mbuf.h>
67 #include <i4b/layer2/i4b_l2.h>
69 /*---------------------------------------------------------------------------*
70 * routine ESTABLISH DATA LINK (Q.921 03/93 page 83)
71 *---------------------------------------------------------------------------*/
73 i4b_establish_data_link(l2_softc_t *l2sc)
75 i4b_l1_activate(l2sc);
77 i4b_clear_exception_conditions(l2sc);
81 i4b_tx_sabme(l2sc, P1);
83 i4b_T200_restart(l2sc);
88 /*---------------------------------------------------------------------------*
89 * routine CLEAR EXCEPTION CONDITIONS (Q.921 03/93 page 83)
90 *---------------------------------------------------------------------------*/
92 i4b_clear_exception_conditions(l2_softc_t *l2sc)
98 /*XXX -------------------------------------------------------------- */
99 /*XXX is this really appropriate here or should it moved elsewhere ? */
101 i4b_Dcleanifq(&l2sc->i_queue);
103 if(l2sc->ua_num != UA_EMPTY)
105 i4b_Dfreembuf(l2sc->ua_frame);
106 l2sc->ua_num = UA_EMPTY;
108 /*XXX -------------------------------------------------------------- */
121 /*---------------------------------------------------------------------------*
122 * routine TRANSMIT ENQUIRE (Q.921 03/93 page 83)
123 *---------------------------------------------------------------------------*/
125 i4b_transmit_enquire(l2_softc_t *l2sc)
128 i4b_tx_rnr_command(l2sc, P1);
130 i4b_tx_rr_command(l2sc, P1);
134 i4b_T200_start(l2sc);
137 /*---------------------------------------------------------------------------*
138 * routine NR ERROR RECOVERY (Q.921 03/93 page 83)
139 *---------------------------------------------------------------------------*/
141 i4b_nr_error_recovery(l2_softc_t *l2sc)
143 i4b_mdl_error_ind(l2sc, "i4b_nr_error_recovery", MDL_ERR_J);
145 i4b_establish_data_link(l2sc);
147 l2sc->l3initiated = 0;
150 /*---------------------------------------------------------------------------*
151 * routine ENQUIRY RESPONSE (Q.921 03/93 page 84)
152 *---------------------------------------------------------------------------*/
154 i4b_enquiry_response(l2_softc_t *l2sc)
157 i4b_tx_rnr_response(l2sc, F1);
159 i4b_tx_rr_response(l2sc, F1);
164 /*---------------------------------------------------------------------------*
165 * routine INVOKE RETRANSMISSION (Q.921 03/93 page 84)
166 *---------------------------------------------------------------------------*/
168 i4b_invoke_retransmission(l2_softc_t *l2sc, int nr)
174 NDBGL2(L2_ERROR, "nr = %d", nr );
176 while(l2sc->vs != nr)
178 NDBGL2(L2_ERROR, "nr(%d) != vs(%d)", nr, l2sc->vs);
182 /* XXXXXXXXXXXXXXXXX */
184 if((l2sc->ua_num != UA_EMPTY) && (l2sc->vs == l2sc->ua_num))
186 if(_IF_QFULL(&l2sc->i_queue))
188 NDBGL2(L2_ERROR, "ERROR, I-queue full!");
192 IF_ENQUEUE(&l2sc->i_queue, l2sc->ua_frame);
193 l2sc->ua_num = UA_EMPTY;
198 NDBGL2(L2_ERROR, "ERROR, l2sc->vs = %d, l2sc->ua_num = %d ",l2sc->vs, l2sc->ua_num);
201 /* XXXXXXXXXXXXXXXXX */
203 i4b_i_frame_queued_up(l2sc);
209 /*---------------------------------------------------------------------------*
210 * routine ACKNOWLEDGE PENDING (Q.921 03/93 p 70)
211 *---------------------------------------------------------------------------*/
213 i4b_acknowledge_pending(l2_softc_t *l2sc)
218 i4b_tx_rr_response(l2sc, F0);
222 /*---------------------------------------------------------------------------*
223 * i4b_print_frame - just print the hex contents of a frame
224 *---------------------------------------------------------------------------*/
226 i4b_print_frame(int len, u_char *buf)
231 if (!(i4b_l2_debug & L2_ERROR)) /* XXXXXXXXXXXXXXXXXXXXX */
234 for(i = 0; i < len; i++)
235 printf(" 0x%x", buf[i]);
240 /*---------------------------------------------------------------------------*
241 * i4b_print_l2var - print some l2softc vars
242 *---------------------------------------------------------------------------*/
244 i4b_print_l2var(l2_softc_t *l2sc)
246 NDBGL2(L2_ERROR, "unit%d V(R)=%d, V(S)=%d, V(A)=%d,ACKP=%d,PBSY=%d,OBSY=%d",
256 /*---------------------------------------------------------------------------*
257 * got s or i frame, check if valid ack for last sent frame
258 *---------------------------------------------------------------------------*/
260 i4b_rxd_ack(l2_softc_t *l2sc, int nr)
264 NDBGL2(L2_ERROR, "N(R)=%d, UA=%d, V(R)=%d, V(S)=%d, V(A)=%d",
272 if(l2sc->ua_num != UA_EMPTY)
280 if(l2sc->ua_num != nr)
281 NDBGL2(L2_ERROR, "((N(R)-1)=%d) != (UA=%d) !!!", nr, l2sc->ua_num);
283 i4b_Dfreembuf(l2sc->ua_frame);
284 l2sc->ua_num = UA_EMPTY;
290 /*---------------------------------------------------------------------------*
291 * if not already active, activate layer 1
292 *---------------------------------------------------------------------------*/
294 i4b_l1_activate(l2_softc_t *l2sc)
296 if(l2sc->ph_active == PH_INACTIVE)
298 l2sc->ph_active = PH_ACTIVEPEND;
299 PH_Act_Req(l2sc->unit);
303 /*---------------------------------------------------------------------------*
304 * check for v(a) <= n(r) <= v(s)
305 * nr = receive sequence frame counter, va = acknowledge sequence frame
306 * counter and vs = transmit sequence frame counter
307 *---------------------------------------------------------------------------*/
309 i4b_l2_nr_ok(int nr, int va, int vs)
311 if((va > nr) && ((nr != 0) || (va != 127)))
313 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [1]", va, nr, vs);
317 if((nr > vs) && ((vs != 0) || (nr != 127)))
319 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [2]", va, nr, vs);
325 #endif /* NI4BQ921 > 0 */