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.6 2005/06/03 16:50:10 dillon Exp $
35 * last edit-date: [Mon May 29 16:55:35 2000]
37 *---------------------------------------------------------------------------*/
39 #if defined(__DragonFly__) || defined(__FreeBSD__)
40 #include "use_i4bq921.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <sys/thread2.h>
53 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
54 #include <sys/callout.h>
57 #if defined(__DragonFly__) || defined(__FreeBSD__)
58 #include <net/i4b/include/machine/i4b_debug.h>
60 #include <i4b/i4b_debug.h>
61 #include <i4b/i4b_ioctl.h>
64 #include "../include/i4b_global.h"
65 #include "../include/i4b_l1l2.h"
66 #include "../include/i4b_mbuf.h"
70 /*---------------------------------------------------------------------------*
71 * routine ESTABLISH DATA LINK (Q.921 03/93 page 83)
72 *---------------------------------------------------------------------------*/
74 i4b_establish_data_link(l2_softc_t *l2sc)
76 i4b_l1_activate(l2sc);
78 i4b_clear_exception_conditions(l2sc);
82 i4b_tx_sabme(l2sc, P1);
84 i4b_T200_restart(l2sc);
89 /*---------------------------------------------------------------------------*
90 * routine CLEAR EXCEPTION CONDITIONS (Q.921 03/93 page 83)
91 *---------------------------------------------------------------------------*/
93 i4b_clear_exception_conditions(l2_softc_t *l2sc)
99 /*XXX -------------------------------------------------------------- */
100 /*XXX is this really appropriate here or should it moved elsewhere ? */
102 i4b_Dcleanifq(&l2sc->i_queue);
104 if(l2sc->ua_num != UA_EMPTY)
106 i4b_Dfreembuf(l2sc->ua_frame);
107 l2sc->ua_num = UA_EMPTY;
109 /*XXX -------------------------------------------------------------- */
122 /*---------------------------------------------------------------------------*
123 * routine TRANSMIT ENQUIRE (Q.921 03/93 page 83)
124 *---------------------------------------------------------------------------*/
126 i4b_transmit_enquire(l2_softc_t *l2sc)
129 i4b_tx_rnr_command(l2sc, P1);
131 i4b_tx_rr_command(l2sc, P1);
135 i4b_T200_start(l2sc);
138 /*---------------------------------------------------------------------------*
139 * routine NR ERROR RECOVERY (Q.921 03/93 page 83)
140 *---------------------------------------------------------------------------*/
142 i4b_nr_error_recovery(l2_softc_t *l2sc)
144 i4b_mdl_error_ind(l2sc, "i4b_nr_error_recovery", MDL_ERR_J);
146 i4b_establish_data_link(l2sc);
148 l2sc->l3initiated = 0;
151 /*---------------------------------------------------------------------------*
152 * routine ENQUIRY RESPONSE (Q.921 03/93 page 84)
153 *---------------------------------------------------------------------------*/
155 i4b_enquiry_response(l2_softc_t *l2sc)
158 i4b_tx_rnr_response(l2sc, F1);
160 i4b_tx_rr_response(l2sc, F1);
165 /*---------------------------------------------------------------------------*
166 * routine INVOKE RETRANSMISSION (Q.921 03/93 page 84)
167 *---------------------------------------------------------------------------*/
169 i4b_invoke_retransmission(l2_softc_t *l2sc, int nr)
175 NDBGL2(L2_ERROR, "nr = %d", nr );
177 while(l2sc->vs != nr)
179 NDBGL2(L2_ERROR, "nr(%d) != vs(%d)", nr, l2sc->vs);
183 /* XXXXXXXXXXXXXXXXX */
185 if((l2sc->ua_num != UA_EMPTY) && (l2sc->vs == l2sc->ua_num))
187 if(IF_QFULL(&l2sc->i_queue))
189 NDBGL2(L2_ERROR, "ERROR, I-queue full!");
193 IF_ENQUEUE(&l2sc->i_queue, l2sc->ua_frame);
194 l2sc->ua_num = UA_EMPTY;
199 NDBGL2(L2_ERROR, "ERROR, l2sc->vs = %d, l2sc->ua_num = %d ",l2sc->vs, l2sc->ua_num);
202 /* XXXXXXXXXXXXXXXXX */
204 i4b_i_frame_queued_up(l2sc);
210 /*---------------------------------------------------------------------------*
211 * routine ACKNOWLEDGE PENDING (Q.921 03/93 p 70)
212 *---------------------------------------------------------------------------*/
214 i4b_acknowledge_pending(l2_softc_t *l2sc)
219 i4b_tx_rr_response(l2sc, F0);
223 /*---------------------------------------------------------------------------*
224 * i4b_print_frame - just print the hex contents of a frame
225 *---------------------------------------------------------------------------*/
227 i4b_print_frame(int len, u_char *buf)
232 if (!(i4b_l2_debug & L2_ERROR)) /* XXXXXXXXXXXXXXXXXXXXX */
235 for(i = 0; i < len; i++)
236 printf(" 0x%x", buf[i]);
241 /*---------------------------------------------------------------------------*
242 * i4b_print_l2var - print some l2softc vars
243 *---------------------------------------------------------------------------*/
245 i4b_print_l2var(l2_softc_t *l2sc)
247 NDBGL2(L2_ERROR, "unit%d V(R)=%d, V(S)=%d, V(A)=%d,ACKP=%d,PBSY=%d,OBSY=%d",
257 /*---------------------------------------------------------------------------*
258 * got s or i frame, check if valid ack for last sent frame
259 *---------------------------------------------------------------------------*/
261 i4b_rxd_ack(l2_softc_t *l2sc, int nr)
265 NDBGL2(L2_ERROR, "N(R)=%d, UA=%d, V(R)=%d, V(S)=%d, V(A)=%d",
273 if(l2sc->ua_num != UA_EMPTY)
281 if(l2sc->ua_num != nr)
282 NDBGL2(L2_ERROR, "((N(R)-1)=%d) != (UA=%d) !!!", nr, l2sc->ua_num);
284 i4b_Dfreembuf(l2sc->ua_frame);
285 l2sc->ua_num = UA_EMPTY;
291 /*---------------------------------------------------------------------------*
292 * if not already active, activate layer 1
293 *---------------------------------------------------------------------------*/
295 i4b_l1_activate(l2_softc_t *l2sc)
297 if(l2sc->ph_active == PH_INACTIVE)
299 l2sc->ph_active = PH_ACTIVEPEND;
300 PH_Act_Req(l2sc->unit);
304 /*---------------------------------------------------------------------------*
305 * check for v(a) <= n(r) <= v(s)
306 * nr = receive sequence frame counter, va = acknowledge sequence frame
307 * counter and vs = transmit sequence frame counter
308 *---------------------------------------------------------------------------*/
310 i4b_l2_nr_ok(int nr, int va, int vs)
312 if((va > nr) && ((nr != 0) || (va != 127)))
314 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [1]", va, nr, vs);
318 if((nr > vs) && ((vs != 0) || (nr != 127)))
320 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [2]", va, nr, vs);
326 #endif /* NI4BQ921 > 0 */