kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / net / i4b / layer2 / i4b_util.c
1 /*
2  * Copyright (c) 1997, 2000 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b_util.c - layer 2 utility routines
28  *      -------------------------------------
29  *
30  *      $Id: i4b_util.c,v 1.26 2000/08/24 11:48:58 hm Exp $ 
31  *
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.3 2003/08/07 21:17:29 dillon Exp $
34  *
35  *      last edit-date: [Mon May 29 16:55:35 2000]
36  *
37  *---------------------------------------------------------------------------*/
38
39 #ifdef __FreeBSD__
40 #include "use_i4bq921.h"
41 #else
42 #define NI4BQ921        1
43 #endif
44 #if NI4BQ921 > 0
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <net/if.h>
51
52 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
53 #include <sys/callout.h>
54 #endif
55
56 #ifdef __FreeBSD__
57 #include <net/i4b/include/machine/i4b_debug.h>
58 #else
59 #include <i4b/i4b_debug.h>
60 #include <i4b/i4b_ioctl.h>
61 #endif
62
63 #include "../include/i4b_global.h"
64 #include "../include/i4b_l1l2.h"
65 #include "../include/i4b_mbuf.h"
66
67 #include "i4b_l2.h"
68
69 /*---------------------------------------------------------------------------*
70  *      routine ESTABLISH DATA LINK (Q.921 03/93 page 83)
71  *---------------------------------------------------------------------------*/
72 void
73 i4b_establish_data_link(l2_softc_t *l2sc)
74 {
75         i4b_l1_activate(l2sc);  
76
77         i4b_clear_exception_conditions(l2sc);
78
79         l2sc->RC = 0;
80
81         i4b_tx_sabme(l2sc, P1);
82
83         i4b_T200_restart(l2sc);
84
85         i4b_T203_stop(l2sc);    
86 }
87
88 /*---------------------------------------------------------------------------*
89  *      routine CLEAR EXCEPTION CONDITIONS (Q.921 03/93 page 83)
90  *---------------------------------------------------------------------------*/
91 void
92 i4b_clear_exception_conditions(l2_softc_t *l2sc)
93 {
94         CRIT_VAR;
95
96         CRIT_BEG;
97         
98 /*XXX -------------------------------------------------------------- */
99 /*XXX is this really appropriate here or should it moved elsewhere ? */
100
101         i4b_Dcleanifq(&l2sc->i_queue);
102         
103         if(l2sc->ua_num != UA_EMPTY)
104         {
105                 i4b_Dfreembuf(l2sc->ua_frame);
106                 l2sc->ua_num = UA_EMPTY;
107         }
108 /*XXX -------------------------------------------------------------- */
109
110         l2sc->peer_busy = 0;
111
112         l2sc->rej_excpt = 0;
113
114         l2sc->own_busy = 0;
115
116         l2sc->ack_pend = 0;     
117
118         CRIT_END;       
119 }
120
121 /*---------------------------------------------------------------------------*
122  *      routine TRANSMIT ENQUIRE (Q.921 03/93 page 83)
123  *---------------------------------------------------------------------------*/
124 void
125 i4b_transmit_enquire(l2_softc_t *l2sc)
126 {
127         if(l2sc->own_busy)
128                 i4b_tx_rnr_command(l2sc, P1);
129         else
130                 i4b_tx_rr_command(l2sc, P1);
131
132         l2sc->ack_pend = 0;
133
134         i4b_T200_start(l2sc);
135 }
136
137 /*---------------------------------------------------------------------------*
138  *      routine NR ERROR RECOVERY (Q.921 03/93 page 83)
139  *---------------------------------------------------------------------------*/
140 void
141 i4b_nr_error_recovery(l2_softc_t *l2sc)
142 {
143         i4b_mdl_error_ind(l2sc, "i4b_nr_error_recovery", MDL_ERR_J);
144
145         i4b_establish_data_link(l2sc);
146         
147         l2sc->l3initiated = 0;
148 }
149
150 /*---------------------------------------------------------------------------*
151  *      routine ENQUIRY RESPONSE (Q.921 03/93 page 84)
152  *---------------------------------------------------------------------------*/
153 void
154 i4b_enquiry_response(l2_softc_t *l2sc)
155 {
156         if(l2sc->own_busy)
157                 i4b_tx_rnr_response(l2sc, F1);
158         else
159                 i4b_tx_rr_response(l2sc, F1);
160
161         l2sc->ack_pend = 0;
162 }
163
164 /*---------------------------------------------------------------------------*
165  *      routine INVOKE RETRANSMISSION (Q.921 03/93 page 84)
166  *---------------------------------------------------------------------------*/
167 void
168 i4b_invoke_retransmission(l2_softc_t *l2sc, int nr)
169 {
170         CRIT_VAR;
171
172         CRIT_BEG;
173
174         NDBGL2(L2_ERROR, "nr = %d", nr );
175         
176         while(l2sc->vs != nr)
177         {
178                 NDBGL2(L2_ERROR, "nr(%d) != vs(%d)", nr, l2sc->vs);
179
180                 M128DEC(l2sc->vs);
181
182 /* XXXXXXXXXXXXXXXXX */
183
184                 if((l2sc->ua_num != UA_EMPTY) && (l2sc->vs == l2sc->ua_num))
185                 {
186                         if(_IF_QFULL(&l2sc->i_queue))
187                         {
188                                 NDBGL2(L2_ERROR, "ERROR, I-queue full!");
189                         }
190                         else
191                         {
192                                 IF_ENQUEUE(&l2sc->i_queue, l2sc->ua_frame);
193                                 l2sc->ua_num = UA_EMPTY;
194                         }
195                 }
196                 else
197                 {
198                         NDBGL2(L2_ERROR, "ERROR, l2sc->vs = %d, l2sc->ua_num = %d ",l2sc->vs, l2sc->ua_num);
199                 }
200
201 /* XXXXXXXXXXXXXXXXX */
202                         
203                 i4b_i_frame_queued_up(l2sc);
204         }
205
206         CRIT_END;
207 }
208
209 /*---------------------------------------------------------------------------*
210  *      routine ACKNOWLEDGE PENDING (Q.921 03/93 p 70)
211  *---------------------------------------------------------------------------*/
212 void
213 i4b_acknowledge_pending(l2_softc_t *l2sc)
214 {
215         if(l2sc->ack_pend)
216         {
217                 l2sc->ack_pend = 0;
218                 i4b_tx_rr_response(l2sc, F0);
219         }
220 }
221
222 /*---------------------------------------------------------------------------*
223  *      i4b_print_frame - just print the hex contents of a frame
224  *---------------------------------------------------------------------------*/
225 void
226 i4b_print_frame(int len, u_char *buf)
227 {
228 #if DO_I4B_DEBUG
229         int i;
230
231         if (!(i4b_l2_debug & L2_ERROR))         /* XXXXXXXXXXXXXXXXXXXXX */
232                 return;
233
234         for(i = 0; i < len; i++)
235                 printf(" 0x%x", buf[i]);
236         printf("\n");
237 #endif
238 }
239
240 /*---------------------------------------------------------------------------*
241  *      i4b_print_l2var - print some l2softc vars
242  *---------------------------------------------------------------------------*/
243 void
244 i4b_print_l2var(l2_softc_t *l2sc)
245 {
246         NDBGL2(L2_ERROR, "unit%d V(R)=%d, V(S)=%d, V(A)=%d,ACKP=%d,PBSY=%d,OBSY=%d",
247                 l2sc->unit,
248                 l2sc->vr,
249                 l2sc->vs,
250                 l2sc->va,
251                 l2sc->ack_pend,
252                 l2sc->peer_busy,
253                 l2sc->own_busy);
254 }
255
256 /*---------------------------------------------------------------------------*
257  *      got s or i frame, check if valid ack for last sent frame
258  *---------------------------------------------------------------------------*/
259 void
260 i4b_rxd_ack(l2_softc_t *l2sc, int nr)
261 {
262
263 #ifdef NOTDEF
264         NDBGL2(L2_ERROR, "N(R)=%d, UA=%d, V(R)=%d, V(S)=%d, V(A)=%d",
265                 nr,
266                 l2sc->ua_num,
267                 l2sc->vr,
268                 l2sc->vs,
269                 l2sc->va);
270 #endif
271
272         if(l2sc->ua_num != UA_EMPTY)
273         {
274                 CRIT_VAR;
275
276                 CRIT_BEG;
277                 
278                 M128DEC(nr);
279
280                 if(l2sc->ua_num != nr)
281                         NDBGL2(L2_ERROR, "((N(R)-1)=%d) != (UA=%d) !!!", nr, l2sc->ua_num);
282                         
283                 i4b_Dfreembuf(l2sc->ua_frame);
284                 l2sc->ua_num = UA_EMPTY;
285                 
286                 CRIT_END;
287         }
288 }
289
290 /*---------------------------------------------------------------------------*
291  *      if not already active, activate layer 1
292  *---------------------------------------------------------------------------*/
293 void
294 i4b_l1_activate(l2_softc_t *l2sc)
295 {
296         if(l2sc->ph_active == PH_INACTIVE)
297         {
298                 l2sc->ph_active = PH_ACTIVEPEND;
299                 PH_Act_Req(l2sc->unit);
300         }
301 };
302
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  *---------------------------------------------------------------------------*/
308 int
309 i4b_l2_nr_ok(int nr, int va, int vs)
310 {
311         if((va > nr) && ((nr != 0) || (va != 127)))
312         {
313                 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [1]", va, nr, vs);
314                 return 0;       /* fail */
315         }
316
317         if((nr > vs) && ((vs != 0) || (nr != 127)))
318         {
319                 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [2]", va, nr, vs);
320                 return 0;       /* fail */
321         }
322         return 1;               /* good */
323 }
324         
325 #endif /* NI4BQ921 > 0 */
326