2 * Copyright (c) 1997, 2002 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
26 /*---------------------------------------------------------------------------
28 * i4b_util.c - layer 2 utility routines
29 * -------------------------------------
30 * last edit-date: [Sat Mar 9 17:55:03 2002]
32 *---------------------------------------------------------------------------*/
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/socket.h>
43 #include <i4b/include/i4b_debug.h>
45 #include <i4b/include/i4b_global.h>
46 #include <i4b/include/i4b_l1l2.h>
47 #include <i4b/include/i4b_mbuf.h>
49 #include <i4b/layer2/i4b_l2.h>
51 /*---------------------------------------------------------------------------*
52 * routine ESTABLISH DATA LINK (Q.921 03/93 page 83)
53 *---------------------------------------------------------------------------*/
55 i4b_establish_data_link(l2_softc_t *l2sc)
57 i4b_l1_activate(l2sc);
59 i4b_clear_exception_conditions(l2sc);
63 i4b_tx_sabme(l2sc, P1);
65 i4b_T200_restart(l2sc);
70 /*---------------------------------------------------------------------------*
71 * routine CLEAR EXCEPTION CONDITIONS (Q.921 03/93 page 83)
72 *---------------------------------------------------------------------------*/
74 i4b_clear_exception_conditions(l2_softc_t *l2sc)
80 /*XXX -------------------------------------------------------------- */
81 /*XXX is this really appropriate here or should it moved elsewhere ? */
83 i4b_Dcleanifq(&l2sc->i_queue);
85 if(l2sc->ua_num != UA_EMPTY)
87 i4b_Dfreembuf(l2sc->ua_frame);
88 l2sc->ua_num = UA_EMPTY;
90 /*XXX -------------------------------------------------------------- */
103 /*---------------------------------------------------------------------------*
104 * routine TRANSMIT ENQUIRE (Q.921 03/93 page 83)
105 *---------------------------------------------------------------------------*/
107 i4b_transmit_enquire(l2_softc_t *l2sc)
110 i4b_tx_rnr_command(l2sc, P1);
112 i4b_tx_rr_command(l2sc, P1);
116 i4b_T200_start(l2sc);
119 /*---------------------------------------------------------------------------*
120 * routine NR ERROR RECOVERY (Q.921 03/93 page 83)
121 *---------------------------------------------------------------------------*/
123 i4b_nr_error_recovery(l2_softc_t *l2sc)
125 i4b_mdl_error_ind(l2sc, "i4b_nr_error_recovery", MDL_ERR_J);
127 i4b_establish_data_link(l2sc);
129 l2sc->l3initiated = 0;
132 /*---------------------------------------------------------------------------*
133 * routine ENQUIRY RESPONSE (Q.921 03/93 page 84)
134 *---------------------------------------------------------------------------*/
136 i4b_enquiry_response(l2_softc_t *l2sc)
139 i4b_tx_rnr_response(l2sc, F1);
141 i4b_tx_rr_response(l2sc, F1);
146 /*---------------------------------------------------------------------------*
147 * routine INVOKE RETRANSMISSION (Q.921 03/93 page 84)
148 *---------------------------------------------------------------------------*/
150 i4b_invoke_retransmission(l2_softc_t *l2sc, int nr)
156 NDBGL2(L2_ERROR, "nr = %d", nr );
158 while(l2sc->vs != nr)
160 NDBGL2(L2_ERROR, "nr(%d) != vs(%d)", nr, l2sc->vs);
164 /* XXXXXXXXXXXXXXXXX */
166 if((l2sc->ua_num != UA_EMPTY) && (l2sc->vs == l2sc->ua_num))
168 if(_IF_QFULL(&l2sc->i_queue))
170 NDBGL2(L2_ERROR, "ERROR, I-queue full!");
174 IF_ENQUEUE(&l2sc->i_queue, l2sc->ua_frame);
175 l2sc->ua_num = UA_EMPTY;
180 NDBGL2(L2_ERROR, "ERROR, l2sc->vs = %d, l2sc->ua_num = %d ",l2sc->vs, l2sc->ua_num);
183 /* XXXXXXXXXXXXXXXXX */
185 i4b_i_frame_queued_up(l2sc);
191 /*---------------------------------------------------------------------------*
192 * routine ACKNOWLEDGE PENDING (Q.921 03/93 p 70)
193 *---------------------------------------------------------------------------*/
195 i4b_acknowledge_pending(l2_softc_t *l2sc)
200 i4b_tx_rr_response(l2sc, F0);
204 /*---------------------------------------------------------------------------*
205 * i4b_print_frame - just print the hex contents of a frame
206 *---------------------------------------------------------------------------*/
208 i4b_print_frame(int len, u_char *buf)
213 if (!(i4b_l2_debug & L2_ERROR)) /* XXXXXXXXXXXXXXXXXXXXX */
216 for(i = 0; i < len; i++)
217 printf(" 0x%x", buf[i]);
222 /*---------------------------------------------------------------------------*
223 * i4b_print_l2var - print some l2softc vars
224 *---------------------------------------------------------------------------*/
226 i4b_print_l2var(l2_softc_t *l2sc)
228 NDBGL2(L2_ERROR, "unit%d V(R)=%d, V(S)=%d, V(A)=%d,ACKP=%d,PBSY=%d,OBSY=%d",
238 /*---------------------------------------------------------------------------*
239 * got s or i frame, check if valid ack for last sent frame
240 *---------------------------------------------------------------------------*/
242 i4b_rxd_ack(l2_softc_t *l2sc, int nr)
246 NDBGL2(L2_ERROR, "N(R)=%d, UA=%d, V(R)=%d, V(S)=%d, V(A)=%d",
254 if(l2sc->ua_num != UA_EMPTY)
262 if(l2sc->ua_num != nr)
263 NDBGL2(L2_ERROR, "((N(R)-1)=%d) != (UA=%d) !!!", nr, l2sc->ua_num);
265 i4b_Dfreembuf(l2sc->ua_frame);
266 l2sc->ua_num = UA_EMPTY;
272 /*---------------------------------------------------------------------------*
273 * if not already active, activate layer 1
274 *---------------------------------------------------------------------------*/
276 i4b_l1_activate(l2_softc_t *l2sc)
278 if(l2sc->ph_active == PH_INACTIVE)
280 l2sc->ph_active = PH_ACTIVEPEND;
281 PH_Act_Req(l2sc->unit);
285 /*---------------------------------------------------------------------------*
286 * check for v(a) <= n(r) <= v(s)
287 * nr = receive sequence frame counter, va = acknowledge sequence frame
288 * counter and vs = transmit sequence frame counter
289 *---------------------------------------------------------------------------*/
291 i4b_l2_nr_ok(int nr, int va, int vs)
293 if((va > nr) && ((nr != 0) || (va != 127)))
295 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [1]", va, nr, vs);
299 if((nr > vs) && ((vs != 0) || (nr != 127)))
301 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [2]", va, nr, vs);